Evaluator.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. //===- Evaluator.cpp - LLVM IR evaluator ----------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Function evaluator for LLVM IR.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/Evaluator.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Analysis/ConstantFolding.h"
  18. #include "llvm/IR/BasicBlock.h"
  19. #include "llvm/IR/Constant.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/DataLayout.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/GlobalAlias.h"
  25. #include "llvm/IR/GlobalValue.h"
  26. #include "llvm/IR/GlobalVariable.h"
  27. #include "llvm/IR/InstrTypes.h"
  28. #include "llvm/IR/Instruction.h"
  29. #include "llvm/IR/Instructions.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/Intrinsics.h"
  32. #include "llvm/IR/Operator.h"
  33. #include "llvm/IR/Type.h"
  34. #include "llvm/IR/User.h"
  35. #include "llvm/IR/Value.h"
  36. #include "llvm/Support/Casting.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <iterator>
  40. #define DEBUG_TYPE "evaluator"
  41. using namespace llvm;
  42. static inline bool
  43. isSimpleEnoughValueToCommit(Constant *C,
  44. SmallPtrSetImpl<Constant *> &SimpleConstants,
  45. const DataLayout &DL);
  46. /// Return true if the specified constant can be handled by the code generator.
  47. /// We don't want to generate something like:
  48. /// void *X = &X/42;
  49. /// because the code generator doesn't have a relocation that can handle that.
  50. ///
  51. /// This function should be called if C was not found (but just got inserted)
  52. /// in SimpleConstants to avoid having to rescan the same constants all the
  53. /// time.
  54. static bool
  55. isSimpleEnoughValueToCommitHelper(Constant *C,
  56. SmallPtrSetImpl<Constant *> &SimpleConstants,
  57. const DataLayout &DL) {
  58. // Simple global addresses are supported, do not allow dllimport or
  59. // thread-local globals.
  60. if (auto *GV = dyn_cast<GlobalValue>(C))
  61. return !GV->hasDLLImportStorageClass() && !GV->isThreadLocal();
  62. // Simple integer, undef, constant aggregate zero, etc are all supported.
  63. if (C->getNumOperands() == 0 || isa<BlockAddress>(C))
  64. return true;
  65. // Aggregate values are safe if all their elements are.
  66. if (isa<ConstantAggregate>(C)) {
  67. for (Value *Op : C->operands())
  68. if (!isSimpleEnoughValueToCommit(cast<Constant>(Op), SimpleConstants, DL))
  69. return false;
  70. return true;
  71. }
  72. // We don't know exactly what relocations are allowed in constant expressions,
  73. // so we allow &global+constantoffset, which is safe and uniformly supported
  74. // across targets.
  75. ConstantExpr *CE = cast<ConstantExpr>(C);
  76. switch (CE->getOpcode()) {
  77. case Instruction::BitCast:
  78. // Bitcast is fine if the casted value is fine.
  79. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  80. case Instruction::IntToPtr:
  81. case Instruction::PtrToInt:
  82. // int <=> ptr is fine if the int type is the same size as the
  83. // pointer type.
  84. if (DL.getTypeSizeInBits(CE->getType()) !=
  85. DL.getTypeSizeInBits(CE->getOperand(0)->getType()))
  86. return false;
  87. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  88. // GEP is fine if it is simple + constant offset.
  89. case Instruction::GetElementPtr:
  90. for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
  91. if (!isa<ConstantInt>(CE->getOperand(i)))
  92. return false;
  93. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  94. case Instruction::Add:
  95. // We allow simple+cst.
  96. if (!isa<ConstantInt>(CE->getOperand(1)))
  97. return false;
  98. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  99. }
  100. return false;
  101. }
  102. static inline bool
  103. isSimpleEnoughValueToCommit(Constant *C,
  104. SmallPtrSetImpl<Constant *> &SimpleConstants,
  105. const DataLayout &DL) {
  106. // If we already checked this constant, we win.
  107. if (!SimpleConstants.insert(C).second)
  108. return true;
  109. // Check the constant.
  110. return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, DL);
  111. }
  112. void Evaluator::MutableValue::clear() {
  113. if (auto *Agg = Val.dyn_cast<MutableAggregate *>())
  114. delete Agg;
  115. Val = nullptr;
  116. }
  117. Constant *Evaluator::MutableValue::read(Type *Ty, APInt Offset,
  118. const DataLayout &DL) const {
  119. TypeSize TySize = DL.getTypeStoreSize(Ty);
  120. const MutableValue *V = this;
  121. while (const auto *Agg = V->Val.dyn_cast<MutableAggregate *>()) {
  122. Type *AggTy = Agg->Ty;
  123. Optional<APInt> Index = DL.getGEPIndexForOffset(AggTy, Offset);
  124. if (!Index || Index->uge(Agg->Elements.size()) ||
  125. !TypeSize::isKnownLE(TySize, DL.getTypeStoreSize(AggTy)))
  126. return nullptr;
  127. V = &Agg->Elements[Index->getZExtValue()];
  128. }
  129. return ConstantFoldLoadFromConst(V->Val.get<Constant *>(), Ty, Offset, DL);
  130. }
  131. bool Evaluator::MutableValue::makeMutable() {
  132. Constant *C = Val.get<Constant *>();
  133. Type *Ty = C->getType();
  134. unsigned NumElements;
  135. if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
  136. NumElements = VT->getNumElements();
  137. } else if (auto *AT = dyn_cast<ArrayType>(Ty))
  138. NumElements = AT->getNumElements();
  139. else if (auto *ST = dyn_cast<StructType>(Ty))
  140. NumElements = ST->getNumElements();
  141. else
  142. return false;
  143. MutableAggregate *MA = new MutableAggregate(Ty);
  144. MA->Elements.reserve(NumElements);
  145. for (unsigned I = 0; I < NumElements; ++I)
  146. MA->Elements.push_back(C->getAggregateElement(I));
  147. Val = MA;
  148. return true;
  149. }
  150. bool Evaluator::MutableValue::write(Constant *V, APInt Offset,
  151. const DataLayout &DL) {
  152. Type *Ty = V->getType();
  153. TypeSize TySize = DL.getTypeStoreSize(Ty);
  154. MutableValue *MV = this;
  155. while (Offset != 0 ||
  156. !CastInst::isBitOrNoopPointerCastable(Ty, MV->getType(), DL)) {
  157. if (MV->Val.is<Constant *>() && !MV->makeMutable())
  158. return false;
  159. MutableAggregate *Agg = MV->Val.get<MutableAggregate *>();
  160. Type *AggTy = Agg->Ty;
  161. Optional<APInt> Index = DL.getGEPIndexForOffset(AggTy, Offset);
  162. if (!Index || Index->uge(Agg->Elements.size()) ||
  163. !TypeSize::isKnownLE(TySize, DL.getTypeStoreSize(AggTy)))
  164. return false;
  165. MV = &Agg->Elements[Index->getZExtValue()];
  166. }
  167. Type *MVType = MV->getType();
  168. MV->clear();
  169. if (Ty->isIntegerTy() && MVType->isPointerTy())
  170. MV->Val = ConstantExpr::getIntToPtr(V, MVType);
  171. else if (Ty->isPointerTy() && MVType->isIntegerTy())
  172. MV->Val = ConstantExpr::getPtrToInt(V, MVType);
  173. else if (Ty != MVType)
  174. MV->Val = ConstantExpr::getBitCast(V, MVType);
  175. else
  176. MV->Val = V;
  177. return true;
  178. }
  179. Constant *Evaluator::MutableAggregate::toConstant() const {
  180. SmallVector<Constant *, 32> Consts;
  181. for (const MutableValue &MV : Elements)
  182. Consts.push_back(MV.toConstant());
  183. if (auto *ST = dyn_cast<StructType>(Ty))
  184. return ConstantStruct::get(ST, Consts);
  185. if (auto *AT = dyn_cast<ArrayType>(Ty))
  186. return ConstantArray::get(AT, Consts);
  187. assert(isa<FixedVectorType>(Ty) && "Must be vector");
  188. return ConstantVector::get(Consts);
  189. }
  190. /// Return the value that would be computed by a load from P after the stores
  191. /// reflected by 'memory' have been performed. If we can't decide, return null.
  192. Constant *Evaluator::ComputeLoadResult(Constant *P, Type *Ty) {
  193. APInt Offset(DL.getIndexTypeSizeInBits(P->getType()), 0);
  194. P = cast<Constant>(P->stripAndAccumulateConstantOffsets(
  195. DL, Offset, /* AllowNonInbounds */ true));
  196. Offset = Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(P->getType()));
  197. auto *GV = dyn_cast<GlobalVariable>(P);
  198. if (!GV)
  199. return nullptr;
  200. auto It = MutatedMemory.find(GV);
  201. if (It != MutatedMemory.end())
  202. return It->second.read(Ty, Offset, DL);
  203. if (!GV->hasDefinitiveInitializer())
  204. return nullptr;
  205. return ConstantFoldLoadFromConst(GV->getInitializer(), Ty, Offset, DL);
  206. }
  207. static Function *getFunction(Constant *C) {
  208. if (auto *Fn = dyn_cast<Function>(C))
  209. return Fn;
  210. if (auto *Alias = dyn_cast<GlobalAlias>(C))
  211. if (auto *Fn = dyn_cast<Function>(Alias->getAliasee()))
  212. return Fn;
  213. return nullptr;
  214. }
  215. Function *
  216. Evaluator::getCalleeWithFormalArgs(CallBase &CB,
  217. SmallVectorImpl<Constant *> &Formals) {
  218. auto *V = CB.getCalledOperand()->stripPointerCasts();
  219. if (auto *Fn = getFunction(getVal(V)))
  220. return getFormalParams(CB, Fn, Formals) ? Fn : nullptr;
  221. return nullptr;
  222. }
  223. bool Evaluator::getFormalParams(CallBase &CB, Function *F,
  224. SmallVectorImpl<Constant *> &Formals) {
  225. if (!F)
  226. return false;
  227. auto *FTy = F->getFunctionType();
  228. if (FTy->getNumParams() > CB.arg_size()) {
  229. LLVM_DEBUG(dbgs() << "Too few arguments for function.\n");
  230. return false;
  231. }
  232. auto ArgI = CB.arg_begin();
  233. for (Type *PTy : FTy->params()) {
  234. auto *ArgC = ConstantFoldLoadThroughBitcast(getVal(*ArgI), PTy, DL);
  235. if (!ArgC) {
  236. LLVM_DEBUG(dbgs() << "Can not convert function argument.\n");
  237. return false;
  238. }
  239. Formals.push_back(ArgC);
  240. ++ArgI;
  241. }
  242. return true;
  243. }
  244. /// If call expression contains bitcast then we may need to cast
  245. /// evaluated return value to a type of the call expression.
  246. Constant *Evaluator::castCallResultIfNeeded(Type *ReturnType, Constant *RV) {
  247. if (!RV || RV->getType() == ReturnType)
  248. return RV;
  249. RV = ConstantFoldLoadThroughBitcast(RV, ReturnType, DL);
  250. if (!RV)
  251. LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
  252. return RV;
  253. }
  254. /// Evaluate all instructions in block BB, returning true if successful, false
  255. /// if we can't evaluate it. NewBB returns the next BB that control flows into,
  256. /// or null upon return. StrippedPointerCastsForAliasAnalysis is set to true if
  257. /// we looked through pointer casts to evaluate something.
  258. bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
  259. bool &StrippedPointerCastsForAliasAnalysis) {
  260. // This is the main evaluation loop.
  261. while (true) {
  262. Constant *InstResult = nullptr;
  263. LLVM_DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
  264. if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
  265. if (!SI->isSimple()) {
  266. LLVM_DEBUG(dbgs() << "Store is not simple! Can not evaluate.\n");
  267. return false; // no volatile/atomic accesses.
  268. }
  269. Constant *Ptr = getVal(SI->getOperand(1));
  270. Constant *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI);
  271. if (Ptr != FoldedPtr) {
  272. LLVM_DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
  273. Ptr = FoldedPtr;
  274. LLVM_DEBUG(dbgs() << "; To: " << *Ptr << "\n");
  275. }
  276. APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
  277. Ptr = cast<Constant>(Ptr->stripAndAccumulateConstantOffsets(
  278. DL, Offset, /* AllowNonInbounds */ true));
  279. Offset = Offset.sextOrTrunc(DL.getIndexTypeSizeInBits(Ptr->getType()));
  280. auto *GV = dyn_cast<GlobalVariable>(Ptr);
  281. if (!GV || !GV->hasUniqueInitializer()) {
  282. LLVM_DEBUG(dbgs() << "Store is not to global with unique initializer: "
  283. << *Ptr << "\n");
  284. return false;
  285. }
  286. // If this might be too difficult for the backend to handle (e.g. the addr
  287. // of one global variable divided by another) then we can't commit it.
  288. Constant *Val = getVal(SI->getOperand(0));
  289. if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, DL)) {
  290. LLVM_DEBUG(dbgs() << "Store value is too complex to evaluate store. "
  291. << *Val << "\n");
  292. return false;
  293. }
  294. auto Res = MutatedMemory.try_emplace(GV, GV->getInitializer());
  295. if (!Res.first->second.write(Val, Offset, DL))
  296. return false;
  297. } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
  298. InstResult = ConstantExpr::get(BO->getOpcode(),
  299. getVal(BO->getOperand(0)),
  300. getVal(BO->getOperand(1)));
  301. LLVM_DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: "
  302. << *InstResult << "\n");
  303. } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
  304. InstResult = ConstantExpr::getCompare(CI->getPredicate(),
  305. getVal(CI->getOperand(0)),
  306. getVal(CI->getOperand(1)));
  307. LLVM_DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult
  308. << "\n");
  309. } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
  310. InstResult = ConstantExpr::getCast(CI->getOpcode(),
  311. getVal(CI->getOperand(0)),
  312. CI->getType());
  313. LLVM_DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult
  314. << "\n");
  315. } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
  316. InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
  317. getVal(SI->getOperand(1)),
  318. getVal(SI->getOperand(2)));
  319. LLVM_DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
  320. << "\n");
  321. } else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
  322. InstResult = ConstantExpr::getExtractValue(
  323. getVal(EVI->getAggregateOperand()), EVI->getIndices());
  324. LLVM_DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: "
  325. << *InstResult << "\n");
  326. } else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {
  327. InstResult = ConstantExpr::getInsertValue(
  328. getVal(IVI->getAggregateOperand()),
  329. getVal(IVI->getInsertedValueOperand()), IVI->getIndices());
  330. LLVM_DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: "
  331. << *InstResult << "\n");
  332. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
  333. Constant *P = getVal(GEP->getOperand(0));
  334. SmallVector<Constant*, 8> GEPOps;
  335. for (Use &Op : llvm::drop_begin(GEP->operands()))
  336. GEPOps.push_back(getVal(Op));
  337. InstResult =
  338. ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps,
  339. cast<GEPOperator>(GEP)->isInBounds());
  340. LLVM_DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult << "\n");
  341. } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
  342. if (!LI->isSimple()) {
  343. LLVM_DEBUG(
  344. dbgs() << "Found a Load! Not a simple load, can not evaluate.\n");
  345. return false; // no volatile/atomic accesses.
  346. }
  347. Constant *Ptr = getVal(LI->getOperand(0));
  348. Constant *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI);
  349. if (Ptr != FoldedPtr) {
  350. Ptr = FoldedPtr;
  351. LLVM_DEBUG(dbgs() << "Found a constant pointer expression, constant "
  352. "folding: "
  353. << *Ptr << "\n");
  354. }
  355. InstResult = ComputeLoadResult(Ptr, LI->getType());
  356. if (!InstResult) {
  357. LLVM_DEBUG(
  358. dbgs() << "Failed to compute load result. Can not evaluate load."
  359. "\n");
  360. return false; // Could not evaluate load.
  361. }
  362. LLVM_DEBUG(dbgs() << "Evaluated load: " << *InstResult << "\n");
  363. } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
  364. if (AI->isArrayAllocation()) {
  365. LLVM_DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
  366. return false; // Cannot handle array allocs.
  367. }
  368. Type *Ty = AI->getAllocatedType();
  369. AllocaTmps.push_back(std::make_unique<GlobalVariable>(
  370. Ty, false, GlobalValue::InternalLinkage, UndefValue::get(Ty),
  371. AI->getName(), /*TLMode=*/GlobalValue::NotThreadLocal,
  372. AI->getType()->getPointerAddressSpace()));
  373. InstResult = AllocaTmps.back().get();
  374. LLVM_DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
  375. } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
  376. CallBase &CB = *cast<CallBase>(&*CurInst);
  377. // Debug info can safely be ignored here.
  378. if (isa<DbgInfoIntrinsic>(CB)) {
  379. LLVM_DEBUG(dbgs() << "Ignoring debug info.\n");
  380. ++CurInst;
  381. continue;
  382. }
  383. // Cannot handle inline asm.
  384. if (CB.isInlineAsm()) {
  385. LLVM_DEBUG(dbgs() << "Found inline asm, can not evaluate.\n");
  386. return false;
  387. }
  388. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CB)) {
  389. if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
  390. if (MSI->isVolatile()) {
  391. LLVM_DEBUG(dbgs() << "Can not optimize a volatile memset "
  392. << "intrinsic.\n");
  393. return false;
  394. }
  395. Constant *Ptr = getVal(MSI->getDest());
  396. Constant *Val = getVal(MSI->getValue());
  397. Constant *DestVal =
  398. ComputeLoadResult(getVal(Ptr), MSI->getValue()->getType());
  399. if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
  400. // This memset is a no-op.
  401. LLVM_DEBUG(dbgs() << "Ignoring no-op memset.\n");
  402. ++CurInst;
  403. continue;
  404. }
  405. }
  406. if (II->isLifetimeStartOrEnd()) {
  407. LLVM_DEBUG(dbgs() << "Ignoring lifetime intrinsic.\n");
  408. ++CurInst;
  409. continue;
  410. }
  411. if (II->getIntrinsicID() == Intrinsic::invariant_start) {
  412. // We don't insert an entry into Values, as it doesn't have a
  413. // meaningful return value.
  414. if (!II->use_empty()) {
  415. LLVM_DEBUG(dbgs()
  416. << "Found unused invariant_start. Can't evaluate.\n");
  417. return false;
  418. }
  419. ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
  420. Value *PtrArg = getVal(II->getArgOperand(1));
  421. Value *Ptr = PtrArg->stripPointerCasts();
  422. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
  423. Type *ElemTy = GV->getValueType();
  424. if (!Size->isMinusOne() &&
  425. Size->getValue().getLimitedValue() >=
  426. DL.getTypeStoreSize(ElemTy)) {
  427. Invariants.insert(GV);
  428. LLVM_DEBUG(dbgs() << "Found a global var that is an invariant: "
  429. << *GV << "\n");
  430. } else {
  431. LLVM_DEBUG(dbgs()
  432. << "Found a global var, but can not treat it as an "
  433. "invariant.\n");
  434. }
  435. }
  436. // Continue even if we do nothing.
  437. ++CurInst;
  438. continue;
  439. } else if (II->getIntrinsicID() == Intrinsic::assume) {
  440. LLVM_DEBUG(dbgs() << "Skipping assume intrinsic.\n");
  441. ++CurInst;
  442. continue;
  443. } else if (II->getIntrinsicID() == Intrinsic::sideeffect) {
  444. LLVM_DEBUG(dbgs() << "Skipping sideeffect intrinsic.\n");
  445. ++CurInst;
  446. continue;
  447. } else if (II->getIntrinsicID() == Intrinsic::pseudoprobe) {
  448. LLVM_DEBUG(dbgs() << "Skipping pseudoprobe intrinsic.\n");
  449. ++CurInst;
  450. continue;
  451. } else {
  452. Value *Stripped = CurInst->stripPointerCastsForAliasAnalysis();
  453. // Only attempt to getVal() if we've actually managed to strip
  454. // anything away, or else we'll call getVal() on the current
  455. // instruction.
  456. if (Stripped != &*CurInst) {
  457. InstResult = getVal(Stripped);
  458. }
  459. if (InstResult) {
  460. LLVM_DEBUG(dbgs()
  461. << "Stripped pointer casts for alias analysis for "
  462. "intrinsic call.\n");
  463. StrippedPointerCastsForAliasAnalysis = true;
  464. InstResult = ConstantExpr::getBitCast(InstResult, II->getType());
  465. } else {
  466. LLVM_DEBUG(dbgs() << "Unknown intrinsic. Cannot evaluate.\n");
  467. return false;
  468. }
  469. }
  470. }
  471. if (!InstResult) {
  472. // Resolve function pointers.
  473. SmallVector<Constant *, 8> Formals;
  474. Function *Callee = getCalleeWithFormalArgs(CB, Formals);
  475. if (!Callee || Callee->isInterposable()) {
  476. LLVM_DEBUG(dbgs() << "Can not resolve function pointer.\n");
  477. return false; // Cannot resolve.
  478. }
  479. if (Callee->isDeclaration()) {
  480. // If this is a function we can constant fold, do it.
  481. if (Constant *C = ConstantFoldCall(&CB, Callee, Formals, TLI)) {
  482. InstResult = castCallResultIfNeeded(CB.getType(), C);
  483. if (!InstResult)
  484. return false;
  485. LLVM_DEBUG(dbgs() << "Constant folded function call. Result: "
  486. << *InstResult << "\n");
  487. } else {
  488. LLVM_DEBUG(dbgs() << "Can not constant fold function call.\n");
  489. return false;
  490. }
  491. } else {
  492. if (Callee->getFunctionType()->isVarArg()) {
  493. LLVM_DEBUG(dbgs()
  494. << "Can not constant fold vararg function call.\n");
  495. return false;
  496. }
  497. Constant *RetVal = nullptr;
  498. // Execute the call, if successful, use the return value.
  499. ValueStack.emplace_back();
  500. if (!EvaluateFunction(Callee, RetVal, Formals)) {
  501. LLVM_DEBUG(dbgs() << "Failed to evaluate function.\n");
  502. return false;
  503. }
  504. ValueStack.pop_back();
  505. InstResult = castCallResultIfNeeded(CB.getType(), RetVal);
  506. if (RetVal && !InstResult)
  507. return false;
  508. if (InstResult) {
  509. LLVM_DEBUG(dbgs() << "Successfully evaluated function. Result: "
  510. << *InstResult << "\n\n");
  511. } else {
  512. LLVM_DEBUG(dbgs()
  513. << "Successfully evaluated function. Result: 0\n\n");
  514. }
  515. }
  516. }
  517. } else if (CurInst->isTerminator()) {
  518. LLVM_DEBUG(dbgs() << "Found a terminator instruction.\n");
  519. if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
  520. if (BI->isUnconditional()) {
  521. NextBB = BI->getSuccessor(0);
  522. } else {
  523. ConstantInt *Cond =
  524. dyn_cast<ConstantInt>(getVal(BI->getCondition()));
  525. if (!Cond) return false; // Cannot determine.
  526. NextBB = BI->getSuccessor(!Cond->getZExtValue());
  527. }
  528. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
  529. ConstantInt *Val =
  530. dyn_cast<ConstantInt>(getVal(SI->getCondition()));
  531. if (!Val) return false; // Cannot determine.
  532. NextBB = SI->findCaseValue(Val)->getCaseSuccessor();
  533. } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
  534. Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
  535. if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
  536. NextBB = BA->getBasicBlock();
  537. else
  538. return false; // Cannot determine.
  539. } else if (isa<ReturnInst>(CurInst)) {
  540. NextBB = nullptr;
  541. } else {
  542. // invoke, unwind, resume, unreachable.
  543. LLVM_DEBUG(dbgs() << "Can not handle terminator.");
  544. return false; // Cannot handle this terminator.
  545. }
  546. // We succeeded at evaluating this block!
  547. LLVM_DEBUG(dbgs() << "Successfully evaluated block.\n");
  548. return true;
  549. } else {
  550. // Did not know how to evaluate this!
  551. LLVM_DEBUG(
  552. dbgs() << "Failed to evaluate block due to unhandled instruction."
  553. "\n");
  554. return false;
  555. }
  556. if (!CurInst->use_empty()) {
  557. InstResult = ConstantFoldConstant(InstResult, DL, TLI);
  558. setVal(&*CurInst, InstResult);
  559. }
  560. // If we just processed an invoke, we finished evaluating the block.
  561. if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
  562. NextBB = II->getNormalDest();
  563. LLVM_DEBUG(dbgs() << "Found an invoke instruction. Finished Block.\n\n");
  564. return true;
  565. }
  566. // Advance program counter.
  567. ++CurInst;
  568. }
  569. }
  570. /// Evaluate a call to function F, returning true if successful, false if we
  571. /// can't evaluate it. ActualArgs contains the formal arguments for the
  572. /// function.
  573. bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
  574. const SmallVectorImpl<Constant*> &ActualArgs) {
  575. // Check to see if this function is already executing (recursion). If so,
  576. // bail out. TODO: we might want to accept limited recursion.
  577. if (is_contained(CallStack, F))
  578. return false;
  579. CallStack.push_back(F);
  580. // Initialize arguments to the incoming values specified.
  581. unsigned ArgNo = 0;
  582. for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
  583. ++AI, ++ArgNo)
  584. setVal(&*AI, ActualArgs[ArgNo]);
  585. // ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
  586. // we can only evaluate any one basic block at most once. This set keeps
  587. // track of what we have executed so we can detect recursive cases etc.
  588. SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
  589. // CurBB - The current basic block we're evaluating.
  590. BasicBlock *CurBB = &F->front();
  591. BasicBlock::iterator CurInst = CurBB->begin();
  592. while (true) {
  593. BasicBlock *NextBB = nullptr; // Initialized to avoid compiler warnings.
  594. LLVM_DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
  595. bool StrippedPointerCastsForAliasAnalysis = false;
  596. if (!EvaluateBlock(CurInst, NextBB, StrippedPointerCastsForAliasAnalysis))
  597. return false;
  598. if (!NextBB) {
  599. // Successfully running until there's no next block means that we found
  600. // the return. Fill it the return value and pop the call stack.
  601. ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
  602. if (RI->getNumOperands()) {
  603. // The Evaluator can look through pointer casts as long as alias
  604. // analysis holds because it's just a simple interpreter and doesn't
  605. // skip memory accesses due to invariant group metadata, but we can't
  606. // let users of Evaluator use a value that's been gleaned looking
  607. // through stripping pointer casts.
  608. if (StrippedPointerCastsForAliasAnalysis &&
  609. !RI->getReturnValue()->getType()->isVoidTy()) {
  610. return false;
  611. }
  612. RetVal = getVal(RI->getOperand(0));
  613. }
  614. CallStack.pop_back();
  615. return true;
  616. }
  617. // Okay, we succeeded in evaluating this control flow. See if we have
  618. // executed the new block before. If so, we have a looping function,
  619. // which we cannot evaluate in reasonable time.
  620. if (!ExecutedBlocks.insert(NextBB).second)
  621. return false; // looped!
  622. // Okay, we have never been in this block before. Check to see if there
  623. // are any PHI nodes. If so, evaluate them with information about where
  624. // we came from.
  625. PHINode *PN = nullptr;
  626. for (CurInst = NextBB->begin();
  627. (PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
  628. setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
  629. // Advance to the next block.
  630. CurBB = NextBB;
  631. }
  632. }