Evaluator.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. /// Return true if this constant is simple enough for us to understand. In
  113. /// particular, if it is a cast to anything other than from one pointer type to
  114. /// another pointer type, we punt. We basically just support direct accesses to
  115. /// globals and GEP's of globals. This should be kept up to date with
  116. /// CommitValueTo.
  117. static bool isSimpleEnoughPointerToCommit(Constant *C) {
  118. // Conservatively, avoid aggregate types. This is because we don't
  119. // want to worry about them partially overlapping other stores.
  120. if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
  121. return false;
  122. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
  123. // Do not allow weak/*_odr/linkonce linkage or external globals.
  124. return GV->hasUniqueInitializer();
  125. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  126. // Handle a constantexpr gep.
  127. if (CE->getOpcode() == Instruction::GetElementPtr &&
  128. isa<GlobalVariable>(CE->getOperand(0)) &&
  129. cast<GEPOperator>(CE)->isInBounds()) {
  130. GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
  131. // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
  132. // external globals.
  133. if (!GV->hasUniqueInitializer())
  134. return false;
  135. // The first index must be zero.
  136. ConstantInt *CI = dyn_cast<ConstantInt>(*std::next(CE->op_begin()));
  137. if (!CI || !CI->isZero()) return false;
  138. // The remaining indices must be compile-time known integers within the
  139. // notional bounds of the corresponding static array types.
  140. if (!CE->isGEPWithNoNotionalOverIndexing())
  141. return false;
  142. return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
  143. // A constantexpr bitcast from a pointer to another pointer is a no-op,
  144. // and we know how to evaluate it by moving the bitcast from the pointer
  145. // operand to the value operand.
  146. } else if (CE->getOpcode() == Instruction::BitCast &&
  147. isa<GlobalVariable>(CE->getOperand(0))) {
  148. // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
  149. // external globals.
  150. return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer();
  151. }
  152. }
  153. return false;
  154. }
  155. /// Apply 'Func' to Ptr. If this returns nullptr, introspect the pointer's
  156. /// type and walk down through the initial elements to obtain additional
  157. /// pointers to try. Returns the first non-null return value from Func, or
  158. /// nullptr if the type can't be introspected further.
  159. static Constant *
  160. evaluateBitcastFromPtr(Constant *Ptr, const DataLayout &DL,
  161. const TargetLibraryInfo *TLI,
  162. std::function<Constant *(Constant *)> Func) {
  163. Constant *Val;
  164. while (!(Val = Func(Ptr))) {
  165. // If Ty is a non-opaque struct, we can convert the pointer to the struct
  166. // into a pointer to its first member.
  167. // FIXME: This could be extended to support arrays as well.
  168. Type *Ty = cast<PointerType>(Ptr->getType())->getElementType();
  169. if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isOpaque())
  170. break;
  171. IntegerType *IdxTy = IntegerType::get(Ty->getContext(), 32);
  172. Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
  173. Constant *const IdxList[] = {IdxZero, IdxZero};
  174. Ptr = ConstantExpr::getGetElementPtr(Ty, Ptr, IdxList);
  175. Ptr = ConstantFoldConstant(Ptr, DL, TLI);
  176. }
  177. return Val;
  178. }
  179. static Constant *getInitializer(Constant *C) {
  180. auto *GV = dyn_cast<GlobalVariable>(C);
  181. return GV && GV->hasDefinitiveInitializer() ? GV->getInitializer() : nullptr;
  182. }
  183. /// Return the value that would be computed by a load from P after the stores
  184. /// reflected by 'memory' have been performed. If we can't decide, return null.
  185. Constant *Evaluator::ComputeLoadResult(Constant *P) {
  186. // If this memory location has been recently stored, use the stored value: it
  187. // is the most up-to-date.
  188. auto findMemLoc = [this](Constant *Ptr) { return MutatedMemory.lookup(Ptr); };
  189. if (Constant *Val = findMemLoc(P))
  190. return Val;
  191. // Access it.
  192. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
  193. if (GV->hasDefinitiveInitializer())
  194. return GV->getInitializer();
  195. return nullptr;
  196. }
  197. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P)) {
  198. switch (CE->getOpcode()) {
  199. // Handle a constantexpr getelementptr.
  200. case Instruction::GetElementPtr:
  201. if (auto *I = getInitializer(CE->getOperand(0)))
  202. return ConstantFoldLoadThroughGEPConstantExpr(I, CE);
  203. break;
  204. // Handle a constantexpr bitcast.
  205. case Instruction::BitCast:
  206. // We're evaluating a load through a pointer that was bitcast to a
  207. // different type. See if the "from" pointer has recently been stored.
  208. // If it hasn't, we may still be able to find a stored pointer by
  209. // introspecting the type.
  210. Constant *Val =
  211. evaluateBitcastFromPtr(CE->getOperand(0), DL, TLI, findMemLoc);
  212. if (!Val)
  213. Val = getInitializer(CE->getOperand(0));
  214. if (Val)
  215. return ConstantFoldLoadThroughBitcast(
  216. Val, P->getType()->getPointerElementType(), DL);
  217. break;
  218. }
  219. }
  220. return nullptr; // don't know how to evaluate.
  221. }
  222. static Function *getFunction(Constant *C) {
  223. if (auto *Fn = dyn_cast<Function>(C))
  224. return Fn;
  225. if (auto *Alias = dyn_cast<GlobalAlias>(C))
  226. if (auto *Fn = dyn_cast<Function>(Alias->getAliasee()))
  227. return Fn;
  228. return nullptr;
  229. }
  230. Function *
  231. Evaluator::getCalleeWithFormalArgs(CallBase &CB,
  232. SmallVectorImpl<Constant *> &Formals) {
  233. auto *V = CB.getCalledOperand();
  234. if (auto *Fn = getFunction(getVal(V)))
  235. return getFormalParams(CB, Fn, Formals) ? Fn : nullptr;
  236. auto *CE = dyn_cast<ConstantExpr>(V);
  237. if (!CE || CE->getOpcode() != Instruction::BitCast ||
  238. !getFormalParams(CB, getFunction(CE->getOperand(0)), Formals))
  239. return nullptr;
  240. return dyn_cast<Function>(
  241. ConstantFoldLoadThroughBitcast(CE, CE->getOperand(0)->getType(), DL));
  242. }
  243. bool Evaluator::getFormalParams(CallBase &CB, Function *F,
  244. SmallVectorImpl<Constant *> &Formals) {
  245. if (!F)
  246. return false;
  247. auto *FTy = F->getFunctionType();
  248. if (FTy->getNumParams() > CB.getNumArgOperands()) {
  249. LLVM_DEBUG(dbgs() << "Too few arguments for function.\n");
  250. return false;
  251. }
  252. auto ArgI = CB.arg_begin();
  253. for (auto ParI = FTy->param_begin(), ParE = FTy->param_end(); ParI != ParE;
  254. ++ParI) {
  255. auto *ArgC = ConstantFoldLoadThroughBitcast(getVal(*ArgI), *ParI, DL);
  256. if (!ArgC) {
  257. LLVM_DEBUG(dbgs() << "Can not convert function argument.\n");
  258. return false;
  259. }
  260. Formals.push_back(ArgC);
  261. ++ArgI;
  262. }
  263. return true;
  264. }
  265. /// If call expression contains bitcast then we may need to cast
  266. /// evaluated return value to a type of the call expression.
  267. Constant *Evaluator::castCallResultIfNeeded(Value *CallExpr, Constant *RV) {
  268. ConstantExpr *CE = dyn_cast<ConstantExpr>(CallExpr);
  269. if (!RV || !CE || CE->getOpcode() != Instruction::BitCast)
  270. return RV;
  271. if (auto *FT =
  272. dyn_cast<FunctionType>(CE->getType()->getPointerElementType())) {
  273. RV = ConstantFoldLoadThroughBitcast(RV, FT->getReturnType(), DL);
  274. if (!RV)
  275. LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
  276. }
  277. return RV;
  278. }
  279. /// Evaluate all instructions in block BB, returning true if successful, false
  280. /// if we can't evaluate it. NewBB returns the next BB that control flows into,
  281. /// or null upon return.
  282. bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
  283. BasicBlock *&NextBB) {
  284. // This is the main evaluation loop.
  285. while (true) {
  286. Constant *InstResult = nullptr;
  287. LLVM_DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
  288. if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
  289. if (!SI->isSimple()) {
  290. LLVM_DEBUG(dbgs() << "Store is not simple! Can not evaluate.\n");
  291. return false; // no volatile/atomic accesses.
  292. }
  293. Constant *Ptr = getVal(SI->getOperand(1));
  294. Constant *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI);
  295. if (Ptr != FoldedPtr) {
  296. LLVM_DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
  297. Ptr = FoldedPtr;
  298. LLVM_DEBUG(dbgs() << "; To: " << *Ptr << "\n");
  299. }
  300. if (!isSimpleEnoughPointerToCommit(Ptr)) {
  301. // If this is too complex for us to commit, reject it.
  302. LLVM_DEBUG(
  303. dbgs() << "Pointer is too complex for us to evaluate store.");
  304. return false;
  305. }
  306. Constant *Val = getVal(SI->getOperand(0));
  307. // If this might be too difficult for the backend to handle (e.g. the addr
  308. // of one global variable divided by another) then we can't commit it.
  309. if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, DL)) {
  310. LLVM_DEBUG(dbgs() << "Store value is too complex to evaluate store. "
  311. << *Val << "\n");
  312. return false;
  313. }
  314. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
  315. if (CE->getOpcode() == Instruction::BitCast) {
  316. LLVM_DEBUG(dbgs()
  317. << "Attempting to resolve bitcast on constant ptr.\n");
  318. // If we're evaluating a store through a bitcast, then we need
  319. // to pull the bitcast off the pointer type and push it onto the
  320. // stored value. In order to push the bitcast onto the stored value,
  321. // a bitcast from the pointer's element type to Val's type must be
  322. // legal. If it's not, we can try introspecting the type to find a
  323. // legal conversion.
  324. auto castValTy = [&](Constant *P) -> Constant * {
  325. Type *Ty = cast<PointerType>(P->getType())->getElementType();
  326. if (Constant *FV = ConstantFoldLoadThroughBitcast(Val, Ty, DL)) {
  327. Ptr = P;
  328. return FV;
  329. }
  330. return nullptr;
  331. };
  332. Constant *NewVal =
  333. evaluateBitcastFromPtr(CE->getOperand(0), DL, TLI, castValTy);
  334. if (!NewVal) {
  335. LLVM_DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
  336. "evaluate.\n");
  337. return false;
  338. }
  339. Val = NewVal;
  340. LLVM_DEBUG(dbgs() << "Evaluated bitcast: " << *Val << "\n");
  341. }
  342. }
  343. MutatedMemory[Ptr] = Val;
  344. } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
  345. InstResult = ConstantExpr::get(BO->getOpcode(),
  346. getVal(BO->getOperand(0)),
  347. getVal(BO->getOperand(1)));
  348. LLVM_DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: "
  349. << *InstResult << "\n");
  350. } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
  351. InstResult = ConstantExpr::getCompare(CI->getPredicate(),
  352. getVal(CI->getOperand(0)),
  353. getVal(CI->getOperand(1)));
  354. LLVM_DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult
  355. << "\n");
  356. } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
  357. InstResult = ConstantExpr::getCast(CI->getOpcode(),
  358. getVal(CI->getOperand(0)),
  359. CI->getType());
  360. LLVM_DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult
  361. << "\n");
  362. } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
  363. InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
  364. getVal(SI->getOperand(1)),
  365. getVal(SI->getOperand(2)));
  366. LLVM_DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
  367. << "\n");
  368. } else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
  369. InstResult = ConstantExpr::getExtractValue(
  370. getVal(EVI->getAggregateOperand()), EVI->getIndices());
  371. LLVM_DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: "
  372. << *InstResult << "\n");
  373. } else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {
  374. InstResult = ConstantExpr::getInsertValue(
  375. getVal(IVI->getAggregateOperand()),
  376. getVal(IVI->getInsertedValueOperand()), IVI->getIndices());
  377. LLVM_DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: "
  378. << *InstResult << "\n");
  379. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
  380. Constant *P = getVal(GEP->getOperand(0));
  381. SmallVector<Constant*, 8> GEPOps;
  382. for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
  383. i != e; ++i)
  384. GEPOps.push_back(getVal(*i));
  385. InstResult =
  386. ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps,
  387. cast<GEPOperator>(GEP)->isInBounds());
  388. LLVM_DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult << "\n");
  389. } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
  390. if (!LI->isSimple()) {
  391. LLVM_DEBUG(
  392. dbgs() << "Found a Load! Not a simple load, can not evaluate.\n");
  393. return false; // no volatile/atomic accesses.
  394. }
  395. Constant *Ptr = getVal(LI->getOperand(0));
  396. Constant *FoldedPtr = ConstantFoldConstant(Ptr, DL, TLI);
  397. if (Ptr != FoldedPtr) {
  398. Ptr = FoldedPtr;
  399. LLVM_DEBUG(dbgs() << "Found a constant pointer expression, constant "
  400. "folding: "
  401. << *Ptr << "\n");
  402. }
  403. InstResult = ComputeLoadResult(Ptr);
  404. if (!InstResult) {
  405. LLVM_DEBUG(
  406. dbgs() << "Failed to compute load result. Can not evaluate load."
  407. "\n");
  408. return false; // Could not evaluate load.
  409. }
  410. LLVM_DEBUG(dbgs() << "Evaluated load: " << *InstResult << "\n");
  411. } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
  412. if (AI->isArrayAllocation()) {
  413. LLVM_DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
  414. return false; // Cannot handle array allocs.
  415. }
  416. Type *Ty = AI->getAllocatedType();
  417. AllocaTmps.push_back(std::make_unique<GlobalVariable>(
  418. Ty, false, GlobalValue::InternalLinkage, UndefValue::get(Ty),
  419. AI->getName(), /*TLMode=*/GlobalValue::NotThreadLocal,
  420. AI->getType()->getPointerAddressSpace()));
  421. InstResult = AllocaTmps.back().get();
  422. LLVM_DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
  423. } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
  424. CallBase &CB = *cast<CallBase>(&*CurInst);
  425. // Debug info can safely be ignored here.
  426. if (isa<DbgInfoIntrinsic>(CB)) {
  427. LLVM_DEBUG(dbgs() << "Ignoring debug info.\n");
  428. ++CurInst;
  429. continue;
  430. }
  431. // Cannot handle inline asm.
  432. if (CB.isInlineAsm()) {
  433. LLVM_DEBUG(dbgs() << "Found inline asm, can not evaluate.\n");
  434. return false;
  435. }
  436. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CB)) {
  437. if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
  438. if (MSI->isVolatile()) {
  439. LLVM_DEBUG(dbgs() << "Can not optimize a volatile memset "
  440. << "intrinsic.\n");
  441. return false;
  442. }
  443. Constant *Ptr = getVal(MSI->getDest());
  444. Constant *Val = getVal(MSI->getValue());
  445. Constant *DestVal = ComputeLoadResult(getVal(Ptr));
  446. if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
  447. // This memset is a no-op.
  448. LLVM_DEBUG(dbgs() << "Ignoring no-op memset.\n");
  449. ++CurInst;
  450. continue;
  451. }
  452. }
  453. if (II->isLifetimeStartOrEnd()) {
  454. LLVM_DEBUG(dbgs() << "Ignoring lifetime intrinsic.\n");
  455. ++CurInst;
  456. continue;
  457. }
  458. if (II->getIntrinsicID() == Intrinsic::invariant_start) {
  459. // We don't insert an entry into Values, as it doesn't have a
  460. // meaningful return value.
  461. if (!II->use_empty()) {
  462. LLVM_DEBUG(dbgs()
  463. << "Found unused invariant_start. Can't evaluate.\n");
  464. return false;
  465. }
  466. ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
  467. Value *PtrArg = getVal(II->getArgOperand(1));
  468. Value *Ptr = PtrArg->stripPointerCasts();
  469. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
  470. Type *ElemTy = GV->getValueType();
  471. if (!Size->isMinusOne() &&
  472. Size->getValue().getLimitedValue() >=
  473. DL.getTypeStoreSize(ElemTy)) {
  474. Invariants.insert(GV);
  475. LLVM_DEBUG(dbgs() << "Found a global var that is an invariant: "
  476. << *GV << "\n");
  477. } else {
  478. LLVM_DEBUG(dbgs()
  479. << "Found a global var, but can not treat it as an "
  480. "invariant.\n");
  481. }
  482. }
  483. // Continue even if we do nothing.
  484. ++CurInst;
  485. continue;
  486. } else if (II->getIntrinsicID() == Intrinsic::assume) {
  487. LLVM_DEBUG(dbgs() << "Skipping assume intrinsic.\n");
  488. ++CurInst;
  489. continue;
  490. } else if (II->getIntrinsicID() == Intrinsic::sideeffect) {
  491. LLVM_DEBUG(dbgs() << "Skipping sideeffect intrinsic.\n");
  492. ++CurInst;
  493. continue;
  494. } else if (II->getIntrinsicID() == Intrinsic::pseudoprobe) {
  495. LLVM_DEBUG(dbgs() << "Skipping pseudoprobe intrinsic.\n");
  496. ++CurInst;
  497. continue;
  498. }
  499. LLVM_DEBUG(dbgs() << "Unknown intrinsic. Can not evaluate.\n");
  500. return false;
  501. }
  502. // Resolve function pointers.
  503. SmallVector<Constant *, 8> Formals;
  504. Function *Callee = getCalleeWithFormalArgs(CB, Formals);
  505. if (!Callee || Callee->isInterposable()) {
  506. LLVM_DEBUG(dbgs() << "Can not resolve function pointer.\n");
  507. return false; // Cannot resolve.
  508. }
  509. if (Callee->isDeclaration()) {
  510. // If this is a function we can constant fold, do it.
  511. if (Constant *C = ConstantFoldCall(&CB, Callee, Formals, TLI)) {
  512. InstResult = castCallResultIfNeeded(CB.getCalledOperand(), C);
  513. if (!InstResult)
  514. return false;
  515. LLVM_DEBUG(dbgs() << "Constant folded function call. Result: "
  516. << *InstResult << "\n");
  517. } else {
  518. LLVM_DEBUG(dbgs() << "Can not constant fold function call.\n");
  519. return false;
  520. }
  521. } else {
  522. if (Callee->getFunctionType()->isVarArg()) {
  523. LLVM_DEBUG(dbgs() << "Can not constant fold vararg function call.\n");
  524. return false;
  525. }
  526. Constant *RetVal = nullptr;
  527. // Execute the call, if successful, use the return value.
  528. ValueStack.emplace_back();
  529. if (!EvaluateFunction(Callee, RetVal, Formals)) {
  530. LLVM_DEBUG(dbgs() << "Failed to evaluate function.\n");
  531. return false;
  532. }
  533. ValueStack.pop_back();
  534. InstResult = castCallResultIfNeeded(CB.getCalledOperand(), RetVal);
  535. if (RetVal && !InstResult)
  536. return false;
  537. if (InstResult) {
  538. LLVM_DEBUG(dbgs() << "Successfully evaluated function. Result: "
  539. << *InstResult << "\n\n");
  540. } else {
  541. LLVM_DEBUG(dbgs()
  542. << "Successfully evaluated function. Result: 0\n\n");
  543. }
  544. }
  545. } else if (CurInst->isTerminator()) {
  546. LLVM_DEBUG(dbgs() << "Found a terminator instruction.\n");
  547. if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
  548. if (BI->isUnconditional()) {
  549. NextBB = BI->getSuccessor(0);
  550. } else {
  551. ConstantInt *Cond =
  552. dyn_cast<ConstantInt>(getVal(BI->getCondition()));
  553. if (!Cond) return false; // Cannot determine.
  554. NextBB = BI->getSuccessor(!Cond->getZExtValue());
  555. }
  556. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
  557. ConstantInt *Val =
  558. dyn_cast<ConstantInt>(getVal(SI->getCondition()));
  559. if (!Val) return false; // Cannot determine.
  560. NextBB = SI->findCaseValue(Val)->getCaseSuccessor();
  561. } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
  562. Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
  563. if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
  564. NextBB = BA->getBasicBlock();
  565. else
  566. return false; // Cannot determine.
  567. } else if (isa<ReturnInst>(CurInst)) {
  568. NextBB = nullptr;
  569. } else {
  570. // invoke, unwind, resume, unreachable.
  571. LLVM_DEBUG(dbgs() << "Can not handle terminator.");
  572. return false; // Cannot handle this terminator.
  573. }
  574. // We succeeded at evaluating this block!
  575. LLVM_DEBUG(dbgs() << "Successfully evaluated block.\n");
  576. return true;
  577. } else {
  578. // Did not know how to evaluate this!
  579. LLVM_DEBUG(
  580. dbgs() << "Failed to evaluate block due to unhandled instruction."
  581. "\n");
  582. return false;
  583. }
  584. if (!CurInst->use_empty()) {
  585. InstResult = ConstantFoldConstant(InstResult, DL, TLI);
  586. setVal(&*CurInst, InstResult);
  587. }
  588. // If we just processed an invoke, we finished evaluating the block.
  589. if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
  590. NextBB = II->getNormalDest();
  591. LLVM_DEBUG(dbgs() << "Found an invoke instruction. Finished Block.\n\n");
  592. return true;
  593. }
  594. // Advance program counter.
  595. ++CurInst;
  596. }
  597. }
  598. /// Evaluate a call to function F, returning true if successful, false if we
  599. /// can't evaluate it. ActualArgs contains the formal arguments for the
  600. /// function.
  601. bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
  602. const SmallVectorImpl<Constant*> &ActualArgs) {
  603. // Check to see if this function is already executing (recursion). If so,
  604. // bail out. TODO: we might want to accept limited recursion.
  605. if (is_contained(CallStack, F))
  606. return false;
  607. CallStack.push_back(F);
  608. // Initialize arguments to the incoming values specified.
  609. unsigned ArgNo = 0;
  610. for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
  611. ++AI, ++ArgNo)
  612. setVal(&*AI, ActualArgs[ArgNo]);
  613. // ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
  614. // we can only evaluate any one basic block at most once. This set keeps
  615. // track of what we have executed so we can detect recursive cases etc.
  616. SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
  617. // CurBB - The current basic block we're evaluating.
  618. BasicBlock *CurBB = &F->front();
  619. BasicBlock::iterator CurInst = CurBB->begin();
  620. while (true) {
  621. BasicBlock *NextBB = nullptr; // Initialized to avoid compiler warnings.
  622. LLVM_DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
  623. if (!EvaluateBlock(CurInst, NextBB))
  624. return false;
  625. if (!NextBB) {
  626. // Successfully running until there's no next block means that we found
  627. // the return. Fill it the return value and pop the call stack.
  628. ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
  629. if (RI->getNumOperands())
  630. RetVal = getVal(RI->getOperand(0));
  631. CallStack.pop_back();
  632. return true;
  633. }
  634. // Okay, we succeeded in evaluating this control flow. See if we have
  635. // executed the new block before. If so, we have a looping function,
  636. // which we cannot evaluate in reasonable time.
  637. if (!ExecutedBlocks.insert(NextBB).second)
  638. return false; // looped!
  639. // Okay, we have never been in this block before. Check to see if there
  640. // are any PHI nodes. If so, evaluate them with information about where
  641. // we came from.
  642. PHINode *PN = nullptr;
  643. for (CurInst = NextBB->begin();
  644. (PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
  645. setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
  646. // Advance to the next block.
  647. CurBB = NextBB;
  648. }
  649. }