Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 1 | //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass statically checks for common and easily-identified constructs |
| 11 | // which produce undefined or likely unintended behavior in LLVM IR. |
| 12 | // |
| 13 | // It is not a guarantee of correctness, in two ways. First, it isn't |
| 14 | // comprehensive. There are checks which could be done statically which are |
| 15 | // not yet implemented. Some of these are indicated by TODO comments, but |
| 16 | // those aren't comprehensive either. Second, many conditions cannot be |
| 17 | // checked statically. This pass does no dynamic instrumentation, so it |
| 18 | // can't check for all possible problems. |
| 19 | // |
| 20 | // Another limitation is that it assumes all code will be executed. A store |
| 21 | // through a null pointer in a basic block which is never reached is harmless, |
Dan Gohman | d3b6e41 | 2010-07-06 15:21:57 +0000 | [diff] [blame] | 22 | // but this pass will warn about it anyway. This is the main reason why most |
| 23 | // of these checks live here instead of in the Verifier pass. |
Dan Gohman | 0883355 | 2010-04-22 01:30:05 +0000 | [diff] [blame] | 24 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 25 | // Optimization passes may make conditions that this pass checks for more or |
| 26 | // less obvious. If an optimization pass appears to be introducing a warning, |
| 27 | // it may be that the optimization pass is merely exposing an existing |
| 28 | // condition in the code. |
| 29 | // |
| 30 | // This code may be run before instcombine. In many cases, instcombine checks |
| 31 | // for the same kinds of things and turns instructions with undefined behavior |
| 32 | // into unreachable (or equivalent). Because of this, this pass makes some |
| 33 | // effort to look through bitcasts and so on. |
| 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #include "llvm/Analysis/Passes.h" |
| 38 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/InstructionSimplify.h" |
| 40 | #include "llvm/Analysis/ConstantFolding.h" |
| 41 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/Lint.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/Loads.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ValueTracking.h" |
| 45 | #include "llvm/Assembly/Writer.h" |
| 46 | #include "llvm/Target/TargetData.h" |
| 47 | #include "llvm/Pass.h" |
| 48 | #include "llvm/PassManager.h" |
| 49 | #include "llvm/IntrinsicInst.h" |
| 50 | #include "llvm/Function.h" |
| 51 | #include "llvm/Support/CallSite.h" |
| 52 | #include "llvm/Support/Debug.h" |
| 53 | #include "llvm/Support/InstVisitor.h" |
| 54 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 56 | using namespace llvm; |
| 57 | |
| 58 | namespace { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 59 | namespace MemRef { |
| 60 | static unsigned Read = 1; |
| 61 | static unsigned Write = 2; |
| 62 | static unsigned Callee = 4; |
| 63 | static unsigned Branchee = 8; |
| 64 | } |
| 65 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 66 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 67 | friend class InstVisitor<Lint>; |
| 68 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 69 | void visitFunction(Function &F); |
| 70 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 71 | void visitCallSite(CallSite CS); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 72 | void visitMemoryReference(Instruction &I, Value *Ptr, |
| 73 | unsigned Size, unsigned Align, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 74 | const Type *Ty, unsigned Flags); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 75 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 76 | void visitCallInst(CallInst &I); |
| 77 | void visitInvokeInst(InvokeInst &I); |
| 78 | void visitReturnInst(ReturnInst &I); |
| 79 | void visitLoadInst(LoadInst &I); |
| 80 | void visitStoreInst(StoreInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 81 | void visitXor(BinaryOperator &I); |
| 82 | void visitSub(BinaryOperator &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 83 | void visitLShr(BinaryOperator &I); |
| 84 | void visitAShr(BinaryOperator &I); |
| 85 | void visitShl(BinaryOperator &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 86 | void visitSDiv(BinaryOperator &I); |
| 87 | void visitUDiv(BinaryOperator &I); |
| 88 | void visitSRem(BinaryOperator &I); |
| 89 | void visitURem(BinaryOperator &I); |
| 90 | void visitAllocaInst(AllocaInst &I); |
| 91 | void visitVAArgInst(VAArgInst &I); |
| 92 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 93 | void visitExtractElementInst(ExtractElementInst &I); |
| 94 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 95 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 96 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 97 | Value *findValue(Value *V, bool OffsetOk) const; |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 98 | Value *findValueImpl(Value *V, bool OffsetOk, |
| 99 | SmallPtrSet<Value *, 4> &Visited) const; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 100 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 101 | public: |
| 102 | Module *Mod; |
| 103 | AliasAnalysis *AA; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 104 | DominatorTree *DT; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 105 | TargetData *TD; |
| 106 | |
| 107 | std::string Messages; |
| 108 | raw_string_ostream MessagesStr; |
| 109 | |
| 110 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 111 | Lint() : FunctionPass(ID), MessagesStr(Messages) {} |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 112 | |
| 113 | virtual bool runOnFunction(Function &F); |
| 114 | |
| 115 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 116 | AU.setPreservesAll(); |
| 117 | AU.addRequired<AliasAnalysis>(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 118 | AU.addRequired<DominatorTree>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 119 | } |
| 120 | virtual void print(raw_ostream &O, const Module *M) const {} |
| 121 | |
| 122 | void WriteValue(const Value *V) { |
| 123 | if (!V) return; |
| 124 | if (isa<Instruction>(V)) { |
| 125 | MessagesStr << *V << '\n'; |
| 126 | } else { |
| 127 | WriteAsOperand(MessagesStr, V, true, Mod); |
| 128 | MessagesStr << '\n'; |
| 129 | } |
| 130 | } |
| 131 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 132 | // CheckFailed - A check failed, so print out the condition and the message |
| 133 | // that failed. This provides a nice place to put a breakpoint if you want |
| 134 | // to see why something is not correct. |
| 135 | void CheckFailed(const Twine &Message, |
| 136 | const Value *V1 = 0, const Value *V2 = 0, |
| 137 | const Value *V3 = 0, const Value *V4 = 0) { |
| 138 | MessagesStr << Message.str() << "\n"; |
| 139 | WriteValue(V1); |
| 140 | WriteValue(V2); |
| 141 | WriteValue(V3); |
| 142 | WriteValue(V4); |
| 143 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | |
| 147 | char Lint::ID = 0; |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 148 | INITIALIZE_PASS(Lint, "lint", "Statically lint-checks LLVM IR", false, true) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 149 | |
| 150 | // Assert - We know that cond should be true, if not print an error message. |
| 151 | #define Assert(C, M) \ |
| 152 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
| 153 | #define Assert1(C, M, V1) \ |
| 154 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
| 155 | #define Assert2(C, M, V1, V2) \ |
| 156 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
| 157 | #define Assert3(C, M, V1, V2, V3) \ |
| 158 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 159 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 160 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
| 161 | |
| 162 | // Lint::run - This is the main Analysis entry point for a |
| 163 | // function. |
| 164 | // |
| 165 | bool Lint::runOnFunction(Function &F) { |
| 166 | Mod = F.getParent(); |
| 167 | AA = &getAnalysis<AliasAnalysis>(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 168 | DT = &getAnalysis<DominatorTree>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 169 | TD = getAnalysisIfAvailable<TargetData>(); |
| 170 | visit(F); |
| 171 | dbgs() << MessagesStr.str(); |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 172 | Messages.clear(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 176 | void Lint::visitFunction(Function &F) { |
| 177 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 178 | // fairly common mistake to neglect to name a function. |
| 179 | Assert1(F.hasName() || F.hasLocalLinkage(), |
| 180 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 181 | |
| 182 | // TODO: Check for irreducible control flow. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void Lint::visitCallSite(CallSite CS) { |
| 186 | Instruction &I = *CS.getInstruction(); |
| 187 | Value *Callee = CS.getCalledValue(); |
| 188 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 189 | visitMemoryReference(I, Callee, ~0u, 0, 0, MemRef::Callee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 190 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 191 | if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 192 | Assert1(CS.getCallingConv() == F->getCallingConv(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 193 | "Undefined behavior: Caller and callee calling convention differ", |
| 194 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 195 | |
| 196 | const FunctionType *FT = F->getFunctionType(); |
| 197 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 198 | |
| 199 | Assert1(FT->isVarArg() ? |
| 200 | FT->getNumParams() <= NumActualArgs : |
| 201 | FT->getNumParams() == NumActualArgs, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 202 | "Undefined behavior: Call argument count mismatches callee " |
| 203 | "argument count", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 204 | |
Dan Gohman | 545d006 | 2010-07-12 18:02:04 +0000 | [diff] [blame] | 205 | Assert1(FT->getReturnType() == I.getType(), |
| 206 | "Undefined behavior: Call return type mismatches " |
| 207 | "callee return type", &I); |
| 208 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 209 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 210 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 211 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 212 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 213 | for (; AI != AE; ++AI) { |
| 214 | Value *Actual = *AI; |
| 215 | if (PI != PE) { |
| 216 | Argument *Formal = PI++; |
| 217 | Assert1(Formal->getType() == Actual->getType(), |
| 218 | "Undefined behavior: Call argument type mismatches " |
| 219 | "callee parameter type", &I); |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 220 | |
| 221 | // Check that noalias arguments don't alias other arguments. The |
| 222 | // AliasAnalysis API isn't expressive enough for what we really want |
| 223 | // to do. Known partial overlap is not distinguished from the case |
| 224 | // where nothing is known. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 225 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 226 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) { |
Dan Gohman | 847a84e | 2010-08-03 00:56:30 +0000 | [diff] [blame] | 227 | Assert1(AI == BI || AA->alias(*AI, *BI) != AliasAnalysis::MustAlias, |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 228 | "Unusual: noalias argument aliases another argument", &I); |
| 229 | } |
| 230 | |
| 231 | // Check that an sret argument points to valid memory. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 232 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
| 233 | const Type *Ty = |
| 234 | cast<PointerType>(Formal->getType())->getElementType(); |
| 235 | visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty), |
| 236 | TD ? TD->getABITypeAlignment(Ty) : 0, |
| 237 | Ty, MemRef::Read | MemRef::Write); |
| 238 | } |
| 239 | } |
| 240 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 243 | if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall()) |
| 244 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 245 | AI != AE; ++AI) { |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 246 | Value *Obj = findValue(*AI, /*OffsetOk=*/true); |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 247 | Assert1(!isa<AllocaInst>(Obj), |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 248 | "Undefined behavior: Call with \"tail\" keyword references " |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 249 | "alloca", &I); |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 252 | |
| 253 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 254 | switch (II->getIntrinsicID()) { |
| 255 | default: break; |
| 256 | |
| 257 | // TODO: Check more intrinsics |
| 258 | |
| 259 | case Intrinsic::memcpy: { |
| 260 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 261 | // TODO: If the size is known, use it. |
| 262 | visitMemoryReference(I, MCI->getDest(), ~0u, MCI->getAlignment(), 0, |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 263 | MemRef::Write); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 264 | visitMemoryReference(I, MCI->getSource(), ~0u, MCI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 265 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 266 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 267 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 268 | // isn't expressive enough for what we really want to do. Known partial |
| 269 | // overlap is not distinguished from the case where nothing is known. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 270 | unsigned Size = 0; |
| 271 | if (const ConstantInt *Len = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 272 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
| 273 | /*OffsetOk=*/false))) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 274 | if (Len->getValue().isIntN(32)) |
| 275 | Size = Len->getValue().getZExtValue(); |
| 276 | Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
| 277 | AliasAnalysis::MustAlias, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 278 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | case Intrinsic::memmove: { |
| 282 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 283 | // TODO: If the size is known, use it. |
| 284 | visitMemoryReference(I, MMI->getDest(), ~0u, MMI->getAlignment(), 0, |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 285 | MemRef::Write); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 286 | visitMemoryReference(I, MMI->getSource(), ~0u, MMI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 287 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | case Intrinsic::memset: { |
| 291 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 292 | // TODO: If the size is known, use it. |
| 293 | visitMemoryReference(I, MSI->getDest(), ~0u, MSI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 294 | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | case Intrinsic::vastart: |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 299 | Assert1(I.getParent()->getParent()->isVarArg(), |
| 300 | "Undefined behavior: va_start called in a non-varargs function", |
| 301 | &I); |
| 302 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 303 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 304 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 305 | break; |
| 306 | case Intrinsic::vacopy: |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 307 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, MemRef::Write); |
| 308 | visitMemoryReference(I, CS.getArgument(1), ~0u, 0, 0, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 309 | break; |
| 310 | case Intrinsic::vaend: |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 311 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 312 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 313 | break; |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 314 | |
| 315 | case Intrinsic::stackrestore: |
| 316 | // Stackrestore doesn't read or write memory, but it sets the |
| 317 | // stack pointer, which the compiler may read from or write to |
| 318 | // at any time, so check it for both readability and writeability. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 319 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 320 | MemRef::Read | MemRef::Write); |
| 321 | break; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | void Lint::visitCallInst(CallInst &I) { |
| 326 | return visitCallSite(&I); |
| 327 | } |
| 328 | |
| 329 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 330 | return visitCallSite(&I); |
| 331 | } |
| 332 | |
| 333 | void Lint::visitReturnInst(ReturnInst &I) { |
| 334 | Function *F = I.getParent()->getParent(); |
| 335 | Assert1(!F->doesNotReturn(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 336 | "Unusual: Return statement in function with noreturn attribute", |
| 337 | &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 338 | |
| 339 | if (Value *V = I.getReturnValue()) { |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 340 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 341 | Assert1(!isa<AllocaInst>(Obj), |
| 342 | "Unusual: Returning alloca value", &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 343 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 346 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 347 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 348 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 349 | Value *Ptr, unsigned Size, unsigned Align, |
| 350 | const Type *Ty, unsigned Flags) { |
| 351 | // If no memory is being referenced, it doesn't matter if the pointer |
| 352 | // is valid. |
| 353 | if (Size == 0) |
| 354 | return; |
| 355 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 356 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 357 | Assert1(!isa<ConstantPointerNull>(UnderlyingObject), |
| 358 | "Undefined behavior: Null pointer dereference", &I); |
| 359 | Assert1(!isa<UndefValue>(UnderlyingObject), |
| 360 | "Undefined behavior: Undef pointer dereference", &I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 361 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 362 | !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(), |
| 363 | "Unusual: All-ones pointer dereference", &I); |
| 364 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 365 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 366 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 367 | |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 368 | if (Flags & MemRef::Write) { |
| 369 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
| 370 | Assert1(!GV->isConstant(), |
| 371 | "Undefined behavior: Write to read-only memory", &I); |
| 372 | Assert1(!isa<Function>(UnderlyingObject) && |
| 373 | !isa<BlockAddress>(UnderlyingObject), |
| 374 | "Undefined behavior: Write to text section", &I); |
| 375 | } |
| 376 | if (Flags & MemRef::Read) { |
| 377 | Assert1(!isa<Function>(UnderlyingObject), |
| 378 | "Unusual: Load from function body", &I); |
| 379 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 380 | "Undefined behavior: Load from block address", &I); |
| 381 | } |
| 382 | if (Flags & MemRef::Callee) { |
| 383 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 384 | "Undefined behavior: Call to block address", &I); |
| 385 | } |
| 386 | if (Flags & MemRef::Branchee) { |
| 387 | Assert1(!isa<Constant>(UnderlyingObject) || |
| 388 | isa<BlockAddress>(UnderlyingObject), |
| 389 | "Undefined behavior: Branch to non-blockaddress", &I); |
| 390 | } |
| 391 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 392 | if (TD) { |
| 393 | if (Align == 0 && Ty) Align = TD->getABITypeAlignment(Ty); |
| 394 | |
| 395 | if (Align != 0) { |
| 396 | unsigned BitWidth = TD->getTypeSizeInBits(Ptr->getType()); |
| 397 | APInt Mask = APInt::getAllOnesValue(BitWidth), |
| 398 | KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 399 | ComputeMaskedBits(Ptr, Mask, KnownZero, KnownOne, TD); |
| 400 | Assert1(!(KnownOne & APInt::getLowBitsSet(BitWidth, Log2_32(Align))), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 401 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 407 | visitMemoryReference(I, I.getPointerOperand(), |
| 408 | AA->getTypeStoreSize(I.getType()), I.getAlignment(), |
| 409 | I.getType(), MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 413 | visitMemoryReference(I, I.getPointerOperand(), |
| 414 | AA->getTypeStoreSize(I.getOperand(0)->getType()), |
| 415 | I.getAlignment(), |
| 416 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 419 | void Lint::visitXor(BinaryOperator &I) { |
| 420 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 421 | !isa<UndefValue>(I.getOperand(1)), |
| 422 | "Undefined result: xor(undef, undef)", &I); |
| 423 | } |
| 424 | |
| 425 | void Lint::visitSub(BinaryOperator &I) { |
| 426 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 427 | !isa<UndefValue>(I.getOperand(1)), |
| 428 | "Undefined result: sub(undef, undef)", &I); |
| 429 | } |
| 430 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 431 | void Lint::visitLShr(BinaryOperator &I) { |
| 432 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 433 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 434 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 435 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void Lint::visitAShr(BinaryOperator &I) { |
| 439 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 440 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 441 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 442 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | void Lint::visitShl(BinaryOperator &I) { |
| 446 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 447 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 448 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 449 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 452 | static bool isZero(Value *V, TargetData *TD) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 453 | // Assume undef could be zero. |
| 454 | if (isa<UndefValue>(V)) return true; |
| 455 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 456 | unsigned BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 457 | APInt Mask = APInt::getAllOnesValue(BitWidth), |
| 458 | KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 459 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD); |
| 460 | return KnownZero.isAllOnesValue(); |
| 461 | } |
| 462 | |
| 463 | void Lint::visitSDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 464 | Assert1(!isZero(I.getOperand(1), TD), |
| 465 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void Lint::visitUDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 469 | Assert1(!isZero(I.getOperand(1), TD), |
| 470 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void Lint::visitSRem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 474 | Assert1(!isZero(I.getOperand(1), TD), |
| 475 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void Lint::visitURem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 479 | Assert1(!isZero(I.getOperand(1), TD), |
| 480 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 484 | if (isa<ConstantInt>(I.getArraySize())) |
| 485 | // This isn't undefined behavior, it's just an obvious pessimization. |
| 486 | Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 487 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 488 | |
| 489 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void Lint::visitVAArgInst(VAArgInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 493 | visitMemoryReference(I, I.getOperand(0), ~0u, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 494 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 498 | visitMemoryReference(I, I.getAddress(), ~0u, 0, 0, MemRef::Branchee); |
Dan Gohman | a8afb2a | 2010-08-02 23:06:43 +0000 | [diff] [blame] | 499 | |
| 500 | Assert1(I.getNumDestinations() != 0, |
| 501 | "Undefined behavior: indirectbr with no destinations", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 504 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
| 505 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 506 | dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 507 | /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 508 | Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 509 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
| 513 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 514 | dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 515 | /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 516 | Assert1(CI->getValue().ult(I.getType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 517 | "Undefined result: insertelement index out of range", &I); |
| 518 | } |
| 519 | |
| 520 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 521 | // This isn't undefined behavior, it's merely suspicious. |
| 522 | Assert1(&I == I.getParent()->begin() || |
| 523 | prior(BasicBlock::iterator(&I))->mayHaveSideEffects(), |
| 524 | "Unusual: unreachable immediately preceded by instruction without " |
| 525 | "side effects", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 528 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 529 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 530 | /// is true, look through getelementptrs with non-zero offsets too. |
| 531 | /// |
| 532 | /// Most analysis passes don't require this logic, because instcombine |
| 533 | /// will simplify most of these kinds of things away. But it's a goal of |
| 534 | /// this Lint pass to be useful even on non-optimized IR. |
| 535 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 536 | SmallPtrSet<Value *, 4> Visited; |
| 537 | return findValueImpl(V, OffsetOk, Visited); |
| 538 | } |
| 539 | |
| 540 | /// findValueImpl - Implementation helper for findValue. |
| 541 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
| 542 | SmallPtrSet<Value *, 4> &Visited) const { |
| 543 | // Detect self-referential values. |
| 544 | if (!Visited.insert(V)) |
| 545 | return UndefValue::get(V->getType()); |
| 546 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 547 | // TODO: Look through sext or zext cast, when the result is known to |
| 548 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 549 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 550 | // TODO: Look through calls with unique return values. |
| 551 | // TODO: Look through vector insert/extract/shuffle. |
| 552 | V = OffsetOk ? V->getUnderlyingObject() : V->stripPointerCasts(); |
| 553 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
| 554 | BasicBlock::iterator BBI = L; |
| 555 | BasicBlock *BB = L->getParent(); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 556 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 557 | for (;;) { |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 558 | if (!VisitedBlocks.insert(BB)) break; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 559 | if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(), |
| 560 | BB, BBI, 6, AA)) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 561 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 562 | if (BBI != BB->begin()) break; |
| 563 | BB = BB->getUniquePredecessor(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 564 | if (!BB) break; |
| 565 | BBI = BB->end(); |
| 566 | } |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 567 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 568 | if (Value *W = PN->hasConstantValue(DT)) |
| 569 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 570 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 571 | if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext()) : |
| 572 | Type::getInt64Ty(V->getContext()))) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 573 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 574 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 575 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
| 576 | Ex->idx_begin(), |
| 577 | Ex->idx_end())) |
| 578 | if (W != V) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 579 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 580 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 581 | // Same as above, but for ConstantExpr instead of Instruction. |
| 582 | if (Instruction::isCast(CE->getOpcode())) { |
| 583 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
| 584 | CE->getOperand(0)->getType(), |
| 585 | CE->getType(), |
| 586 | TD ? TD->getIntPtrType(V->getContext()) : |
| 587 | Type::getInt64Ty(V->getContext()))) |
| 588 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
| 589 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
| 590 | const SmallVector<unsigned, 4> &Indices = CE->getIndices(); |
| 591 | if (Value *W = FindInsertedValue(CE->getOperand(0), |
| 592 | Indices.begin(), |
| 593 | Indices.end())) |
| 594 | if (W != V) |
| 595 | return findValueImpl(W, OffsetOk, Visited); |
| 596 | } |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // As a last resort, try SimplifyInstruction or constant folding. |
| 600 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
| 601 | if (Value *W = SimplifyInstruction(Inst, TD)) |
| 602 | if (W != Inst) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 603 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 604 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 605 | if (Value *W = ConstantFoldConstantExpression(CE, TD)) |
| 606 | if (W != V) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 607 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | return V; |
| 611 | } |
| 612 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 613 | //===----------------------------------------------------------------------===// |
| 614 | // Implement the public interfaces to this file... |
| 615 | //===----------------------------------------------------------------------===// |
| 616 | |
| 617 | FunctionPass *llvm::createLintPass() { |
| 618 | return new Lint(); |
| 619 | } |
| 620 | |
| 621 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 622 | /// |
| 623 | void llvm::lintFunction(const Function &f) { |
| 624 | Function &F = const_cast<Function&>(f); |
| 625 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 626 | |
| 627 | FunctionPassManager FPM(F.getParent()); |
| 628 | Lint *V = new Lint(); |
| 629 | FPM.add(V); |
| 630 | FPM.run(F); |
| 631 | } |
| 632 | |
| 633 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 634 | /// |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 635 | void llvm::lintModule(const Module &M) { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 636 | PassManager PM; |
| 637 | Lint *V = new Lint(); |
| 638 | PM.add(V); |
| 639 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 640 | } |