Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Support/CallSite.h" |
| 21 | using namespace llvm; |
| 22 | |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 23 | unsigned CallSite::getCallingConv() const { |
| 24 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 25 | return CI->getCallingConv(); |
| 26 | else |
| 27 | return cast<InvokeInst>(I)->getCallingConv(); |
| 28 | } |
| 29 | void CallSite::setCallingConv(unsigned CC) { |
| 30 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 31 | CI->setCallingConv(CC); |
| 32 | else |
| 33 | cast<InvokeInst>(I)->setCallingConv(CC); |
| 34 | } |
| 35 | |
| 36 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 37 | |
| 38 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 40 | // TerminatorInst Class |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 44 | Use *Ops, unsigned NumOps, Instruction *IB) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 45 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) { |
| 46 | } |
| 47 | |
| 48 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
| 49 | Use *Ops, unsigned NumOps, BasicBlock *IAE) |
| 50 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) { |
| 51 | } |
| 52 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 53 | // Out of line virtual method, so the vtable, etc has a home. |
| 54 | TerminatorInst::~TerminatorInst() { |
| 55 | } |
| 56 | |
| 57 | // Out of line virtual method, so the vtable, etc has a home. |
| 58 | UnaryInstruction::~UnaryInstruction() { |
| 59 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // PHINode Class |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
| 66 | PHINode::PHINode(const PHINode &PN) |
| 67 | : Instruction(PN.getType(), Instruction::PHI, |
| 68 | new Use[PN.getNumOperands()], PN.getNumOperands()), |
| 69 | ReservedSpace(PN.getNumOperands()) { |
| 70 | Use *OL = OperandList; |
| 71 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
| 72 | OL[i].init(PN.getOperand(i), this); |
| 73 | OL[i+1].init(PN.getOperand(i+1), this); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | PHINode::~PHINode() { |
| 78 | delete [] OperandList; |
| 79 | } |
| 80 | |
| 81 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 82 | // predecessor basic block is deleted. |
| 83 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 84 | unsigned NumOps = getNumOperands(); |
| 85 | Use *OL = OperandList; |
| 86 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 87 | Value *Removed = OL[Idx*2]; |
| 88 | |
| 89 | // Move everything after this operand down. |
| 90 | // |
| 91 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 92 | // client might not expect this to happen. The code as it is thrashes the |
| 93 | // use/def lists, which is kinda lame. |
| 94 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 95 | OL[i-2] = OL[i]; |
| 96 | OL[i-2+1] = OL[i+1]; |
| 97 | } |
| 98 | |
| 99 | // Nuke the last value. |
| 100 | OL[NumOps-2].set(0); |
| 101 | OL[NumOps-2+1].set(0); |
| 102 | NumOperands = NumOps-2; |
| 103 | |
| 104 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 105 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 106 | // If anyone is using this PHI, make them use a dummy value instead... |
| 107 | replaceAllUsesWith(UndefValue::get(getType())); |
| 108 | eraseFromParent(); |
| 109 | } |
| 110 | return Removed; |
| 111 | } |
| 112 | |
| 113 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 114 | /// list according to the following behavior: |
| 115 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 116 | /// of operation. This grows the number of ops by 1.5 times. |
| 117 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 118 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 119 | /// |
| 120 | void PHINode::resizeOperands(unsigned NumOps) { |
| 121 | if (NumOps == 0) { |
| 122 | NumOps = (getNumOperands())*3/2; |
| 123 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 124 | } else if (NumOps*2 > NumOperands) { |
| 125 | // No resize needed. |
| 126 | if (ReservedSpace >= NumOps) return; |
| 127 | } else if (NumOps == NumOperands) { |
| 128 | if (ReservedSpace == NumOps) return; |
| 129 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 130 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ReservedSpace = NumOps; |
| 134 | Use *NewOps = new Use[NumOps]; |
| 135 | Use *OldOps = OperandList; |
| 136 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 137 | NewOps[i].init(OldOps[i], this); |
| 138 | OldOps[i].set(0); |
| 139 | } |
| 140 | delete [] OldOps; |
| 141 | OperandList = NewOps; |
| 142 | } |
| 143 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 144 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 145 | /// value, return the value, otherwise return null. |
| 146 | /// |
Chris Lattner | 1d8b248 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 147 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 148 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 149 | if (getNumIncomingValues() == 1) |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 150 | if (getIncomingValue(0) != this) // not X = phi X |
| 151 | return getIncomingValue(0); |
| 152 | else |
| 153 | return UndefValue::get(getType()); // Self cycle is dead. |
| 154 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 155 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 156 | // the PHI node with the incoming value. |
| 157 | // |
| 158 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 159 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 160 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 161 | if (isa<UndefValue>(getIncomingValue(i))) |
| 162 | HasUndefInput = true; |
| 163 | else if (getIncomingValue(i) != this) // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 164 | if (InVal && getIncomingValue(i) != InVal) |
| 165 | return 0; // Not the same, bail out. |
| 166 | else |
| 167 | InVal = getIncomingValue(i); |
| 168 | |
| 169 | // The only case that could cause InVal to be null is if we have a PHI node |
| 170 | // that only has entries for itself. In this case, there is no entry into the |
| 171 | // loop, so kill the PHI. |
| 172 | // |
| 173 | if (InVal == 0) InVal = UndefValue::get(getType()); |
| 174 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 175 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 176 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 177 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 178 | // or if the client is prepared to deal with this possibility. |
| 179 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 180 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 181 | // If it's in the entry block, it dominates everything. |
Chris Lattner | 37774af | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 182 | if (IV->getParent() != &IV->getParent()->getParent()->front() || |
| 183 | isa<InvokeInst>(IV)) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 184 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 185 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 186 | // All of the incoming values are the same, return the value now. |
| 187 | return InVal; |
| 188 | } |
| 189 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 190 | |
| 191 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 192 | // CallInst Implementation |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 195 | CallInst::~CallInst() { |
| 196 | delete [] OperandList; |
| 197 | } |
| 198 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 199 | void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { |
| 200 | NumOperands = NumParams+1; |
| 201 | Use *OL = OperandList = new Use[NumParams+1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 202 | OL[0].init(Func, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 203 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 204 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 205 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 206 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 208 | assert((NumParams == FTy->getNumParams() || |
| 209 | (FTy->isVarArg() && NumParams > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 210 | "Calling a function with bad signature!"); |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 211 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 212 | assert((i >= FTy->getNumParams() || |
| 213 | FTy->getParamType(i) == Params[i]->getType()) && |
| 214 | "Calling a function with a bad signature!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 215 | OL[i+1].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 216 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 219 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
| 220 | NumOperands = 3; |
| 221 | Use *OL = OperandList = new Use[3]; |
| 222 | OL[0].init(Func, this); |
| 223 | OL[1].init(Actual1, this); |
| 224 | OL[2].init(Actual2, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 225 | |
| 226 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 227 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 228 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 229 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 230 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 231 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 232 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 233 | assert((0 >= FTy->getNumParams() || |
| 234 | FTy->getParamType(0) == Actual1->getType()) && |
| 235 | "Calling a function with a bad signature!"); |
| 236 | assert((1 >= FTy->getNumParams() || |
| 237 | FTy->getParamType(1) == Actual2->getType()) && |
| 238 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 241 | void CallInst::init(Value *Func, Value *Actual) { |
| 242 | NumOperands = 2; |
| 243 | Use *OL = OperandList = new Use[2]; |
| 244 | OL[0].init(Func, this); |
| 245 | OL[1].init(Actual, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 246 | |
| 247 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 248 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 249 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 250 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 251 | assert((FTy->getNumParams() == 1 || |
| 252 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 253 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 254 | assert((0 == FTy->getNumParams() || |
| 255 | FTy->getParamType(0) == Actual->getType()) && |
| 256 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 259 | void CallInst::init(Value *Func) { |
| 260 | NumOperands = 1; |
| 261 | Use *OL = OperandList = new Use[1]; |
| 262 | OL[0].init(Func, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 263 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 264 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 265 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 266 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 267 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 268 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 271 | CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 272 | const std::string &Name, BasicBlock *InsertAtEnd) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 273 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 274 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 275 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 276 | init(Func, Args, NumArgs); |
| 277 | } |
| 278 | CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, |
| 279 | const std::string &Name, Instruction *InsertBefore) |
| 280 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 281 | ->getElementType())->getReturnType(), |
| 282 | Instruction::Call, 0, 0, Name, InsertBefore) { |
| 283 | init(Func, Args, NumArgs); |
| 284 | } |
| 285 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 286 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 287 | const std::string &Name, Instruction *InsertBefore) |
| 288 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 289 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 290 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 291 | init(Func, Actual1, Actual2); |
| 292 | } |
| 293 | |
| 294 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 295 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 296 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 297 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 298 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 299 | init(Func, Actual1, Actual2); |
| 300 | } |
| 301 | |
| 302 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 303 | Instruction *InsertBefore) |
| 304 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 305 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 306 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 307 | init(Func, Actual); |
| 308 | } |
| 309 | |
| 310 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 311 | BasicBlock *InsertAtEnd) |
| 312 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 313 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 314 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 315 | init(Func, Actual); |
| 316 | } |
| 317 | |
| 318 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 319 | Instruction *InsertBefore) |
| 320 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 321 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 322 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 323 | init(Func); |
| 324 | } |
| 325 | |
| 326 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 327 | BasicBlock *InsertAtEnd) |
| 328 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 329 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 330 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 331 | init(Func); |
| 332 | } |
| 333 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 334 | CallInst::CallInst(const CallInst &CI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 335 | : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], |
| 336 | CI.getNumOperands()) { |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 337 | SubclassData = CI.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 338 | Use *OL = OperandList; |
| 339 | Use *InOL = CI.OperandList; |
| 340 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
| 341 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 344 | |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | // InvokeInst Implementation |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 349 | InvokeInst::~InvokeInst() { |
| 350 | delete [] OperandList; |
| 351 | } |
| 352 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 353 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 354 | Value* const *Args, unsigned NumArgs) { |
| 355 | NumOperands = 3+NumArgs; |
| 356 | Use *OL = OperandList = new Use[3+NumArgs]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 357 | OL[0].init(Fn, this); |
| 358 | OL[1].init(IfNormal, this); |
| 359 | OL[2].init(IfException, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 360 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 361 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 362 | FTy = FTy; // silence warning. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 363 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 364 | assert((NumArgs == FTy->getNumParams()) || |
| 365 | (FTy->isVarArg() && NumArgs > FTy->getNumParams()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 366 | "Calling a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 367 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 368 | for (unsigned i = 0, e = NumArgs; i != e; i++) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 369 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 370 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 371 | "Invoking a function with a bad signature!"); |
| 372 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 373 | OL[i+3].init(Args[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 374 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 378 | BasicBlock *IfException, |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 379 | Value* const *Args, unsigned NumArgs, |
| 380 | const std::string &Name, Instruction *InsertBefore) |
| 381 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 382 | ->getElementType())->getReturnType(), |
| 383 | Instruction::Invoke, 0, 0, Name, InsertBefore) { |
| 384 | init(Fn, IfNormal, IfException, Args, NumArgs); |
| 385 | } |
| 386 | |
| 387 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 388 | BasicBlock *IfException, |
| 389 | Value* const *Args, unsigned NumArgs, |
| 390 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 391 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 392 | ->getElementType())->getReturnType(), |
| 393 | Instruction::Invoke, 0, 0, Name, InsertAtEnd) { |
| 394 | init(Fn, IfNormal, IfException, Args, NumArgs); |
| 395 | } |
| 396 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 397 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 398 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 399 | new Use[II.getNumOperands()], II.getNumOperands()) { |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 400 | SubclassData = II.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 401 | Use *OL = OperandList, *InOL = II.OperandList; |
| 402 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
| 403 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 406 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 407 | return getSuccessor(idx); |
| 408 | } |
| 409 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 410 | return getNumSuccessors(); |
| 411 | } |
| 412 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 413 | return setSuccessor(idx, B); |
| 414 | } |
| 415 | |
| 416 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 417 | //===----------------------------------------------------------------------===// |
| 418 | // ReturnInst Implementation |
| 419 | //===----------------------------------------------------------------------===// |
| 420 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 421 | void ReturnInst::init(Value *retVal) { |
| 422 | if (retVal && retVal->getType() != Type::VoidTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 423 | assert(!isa<BasicBlock>(retVal) && |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 424 | "Cannot return basic block. Probably using the incorrect ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 425 | NumOperands = 1; |
| 426 | RetVal.init(retVal, this); |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 430 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 431 | return getNumSuccessors(); |
| 432 | } |
| 433 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 434 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 435 | // emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 436 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 437 | assert(0 && "ReturnInst has no successors!"); |
| 438 | } |
| 439 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 440 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
| 441 | assert(0 && "ReturnInst has no successors!"); |
| 442 | abort(); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 447 | //===----------------------------------------------------------------------===// |
| 448 | // UnwindInst Implementation |
| 449 | //===----------------------------------------------------------------------===// |
| 450 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 451 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 452 | return getNumSuccessors(); |
| 453 | } |
| 454 | |
| 455 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 456 | assert(0 && "UnwindInst has no successors!"); |
| 457 | } |
| 458 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 459 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
| 460 | assert(0 && "UnwindInst has no successors!"); |
| 461 | abort(); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 465 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 466 | // UnreachableInst Implementation |
| 467 | //===----------------------------------------------------------------------===// |
| 468 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 469 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 470 | return getNumSuccessors(); |
| 471 | } |
| 472 | |
| 473 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 474 | assert(0 && "UnwindInst has no successors!"); |
| 475 | } |
| 476 | |
| 477 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
| 478 | assert(0 && "UnwindInst has no successors!"); |
| 479 | abort(); |
| 480 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 484 | // BranchInst Implementation |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 487 | void BranchInst::AssertOK() { |
| 488 | if (isConditional()) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 489 | assert(getCondition()->getType() == Type::Int1Ty && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 490 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 493 | BranchInst::BranchInst(const BranchInst &BI) : |
| 494 | TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) { |
| 495 | OperandList[0].init(BI.getOperand(0), this); |
| 496 | if (BI.getNumOperands() != 1) { |
| 497 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
| 498 | OperandList[1].init(BI.getOperand(1), this); |
| 499 | OperandList[2].init(BI.getOperand(2), this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 503 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 504 | return getSuccessor(idx); |
| 505 | } |
| 506 | unsigned BranchInst::getNumSuccessorsV() const { |
| 507 | return getNumSuccessors(); |
| 508 | } |
| 509 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 510 | setSuccessor(idx, B); |
| 511 | } |
| 512 | |
| 513 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 514 | //===----------------------------------------------------------------------===// |
| 515 | // AllocationInst Implementation |
| 516 | //===----------------------------------------------------------------------===// |
| 517 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 518 | static Value *getAISize(Value *Amt) { |
| 519 | if (!Amt) |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 520 | Amt = ConstantInt::get(Type::Int32Ty, 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 521 | else { |
| 522 | assert(!isa<BasicBlock>(Amt) && |
| 523 | "Passed basic block into allocation size parameter! Ue other ctor"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 524 | assert(Amt->getType() == Type::Int32Ty && |
Reid Spencer | 7e16e23 | 2007-01-26 06:30:34 +0000 | [diff] [blame] | 525 | "Malloc/Allocation array size is not a 32-bit integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 526 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 527 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 530 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 531 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 532 | Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 533 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 534 | 0, InsertBefore), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 535 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 536 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 537 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 540 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 541 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 542 | BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 543 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 544 | 0, InsertAtEnd), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 545 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 546 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 547 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 550 | // Out of line virtual method, so the vtable, etc has a home. |
| 551 | AllocationInst::~AllocationInst() { |
| 552 | } |
| 553 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 554 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 555 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0))) |
| 556 | return CUI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 557 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | const Type *AllocationInst::getAllocatedType() const { |
| 561 | return getType()->getElementType(); |
| 562 | } |
| 563 | |
| 564 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 565 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 566 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | MallocInst::MallocInst(const MallocInst &MI) |
| 570 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 571 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | //===----------------------------------------------------------------------===// |
| 575 | // FreeInst Implementation |
| 576 | //===----------------------------------------------------------------------===// |
| 577 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 578 | void FreeInst::AssertOK() { |
| 579 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 580 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 584 | : UnaryInstruction(Type::VoidTy, Free, Ptr, 0, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 585 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 589 | : UnaryInstruction(Type::VoidTy, Free, Ptr, 0, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 590 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
| 594 | //===----------------------------------------------------------------------===// |
| 595 | // LoadInst Implementation |
| 596 | //===----------------------------------------------------------------------===// |
| 597 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 598 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 599 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 600 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 604 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 605 | Load, Ptr, 0, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 606 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 607 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 608 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 612 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 613 | Load, Ptr, 0, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 614 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 615 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 616 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 620 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 621 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 622 | Load, Ptr, 0, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 623 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 624 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 625 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 629 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 630 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 631 | Load, Ptr, 0, InsertAE) { |
| 632 | setVolatile(isVolatile); |
| 633 | AssertOK(); |
| 634 | setName(Name); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | |
| 639 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 640 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 641 | Load, Ptr, Name, InsertBef) { |
| 642 | setVolatile(false); |
| 643 | AssertOK(); |
| 644 | } |
| 645 | |
| 646 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 647 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 648 | Load, Ptr, Name, InsertAE) { |
| 649 | setVolatile(false); |
| 650 | AssertOK(); |
| 651 | } |
| 652 | |
| 653 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 654 | Instruction *InsertBef) |
| 655 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 656 | Load, Ptr, Name, InsertBef) { |
| 657 | setVolatile(isVolatile); |
| 658 | AssertOK(); |
| 659 | } |
| 660 | |
| 661 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 662 | BasicBlock *InsertAE) |
| 663 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 664 | Load, Ptr, Name, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 665 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 666 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | |
| 670 | //===----------------------------------------------------------------------===// |
| 671 | // StoreInst Implementation |
| 672 | //===----------------------------------------------------------------------===// |
| 673 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 674 | void StoreInst::AssertOK() { |
| 675 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 676 | "Ptr must have pointer type!"); |
| 677 | assert(getOperand(0)->getType() == |
| 678 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 679 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 680 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 681 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 682 | |
| 683 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 684 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 685 | Ops[0].init(val, this); |
| 686 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 687 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 688 | AssertOK(); |
| 689 | } |
| 690 | |
| 691 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 692 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 693 | Ops[0].init(val, this); |
| 694 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 695 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 696 | AssertOK(); |
| 697 | } |
| 698 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 699 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 700 | Instruction *InsertBefore) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 701 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 702 | Ops[0].init(val, this); |
| 703 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 704 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 705 | AssertOK(); |
| 706 | } |
| 707 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 708 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 709 | BasicBlock *InsertAtEnd) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 710 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 711 | Ops[0].init(val, this); |
| 712 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 713 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 714 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | // GetElementPtrInst Implementation |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | |
| 721 | // checkType - Simple wrapper function to give a better assertion failure |
| 722 | // message on bad indexes for a gep instruction. |
| 723 | // |
| 724 | static inline const Type *checkType(const Type *Ty) { |
Chris Lattner | 47a6e63 | 2006-05-14 18:34:36 +0000 | [diff] [blame] | 725 | assert(Ty && "Invalid GetElementPtrInst indices for type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 726 | return Ty; |
| 727 | } |
| 728 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 729 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { |
| 730 | NumOperands = 1+NumIdx; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 731 | Use *OL = OperandList = new Use[NumOperands]; |
| 732 | OL[0].init(Ptr, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 734 | for (unsigned i = 0; i != NumIdx; ++i) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 735 | OL[i+1].init(Idx[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 739 | NumOperands = 3; |
| 740 | Use *OL = OperandList = new Use[3]; |
| 741 | OL[0].init(Ptr, this); |
| 742 | OL[1].init(Idx0, this); |
| 743 | OL[2].init(Idx1, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 746 | void GetElementPtrInst::init(Value *Ptr, Value *Idx) { |
| 747 | NumOperands = 2; |
| 748 | Use *OL = OperandList = new Use[2]; |
| 749 | OL[0].init(Ptr, this); |
| 750 | OL[1].init(Idx, this); |
| 751 | } |
| 752 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 753 | |
| 754 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, |
| 755 | unsigned NumIdx, |
| 756 | const std::string &Name, Instruction *InBe) |
| 757 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
Reid Spencer | dee14b5 | 2007-01-31 22:30:26 +0000 | [diff] [blame] | 758 | Idx, NumIdx, true))), |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 759 | GetElementPtr, 0, 0, Name, InBe) { |
| 760 | init(Ptr, Idx, NumIdx); |
| 761 | } |
| 762 | |
| 763 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, |
| 764 | unsigned NumIdx, |
| 765 | const std::string &Name, BasicBlock *IAE) |
| 766 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
Reid Spencer | dee14b5 | 2007-01-31 22:30:26 +0000 | [diff] [blame] | 767 | Idx, NumIdx, true))), |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 768 | GetElementPtr, 0, 0, Name, IAE) { |
| 769 | init(Ptr, Idx, NumIdx); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 772 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 773 | const std::string &Name, Instruction *InBe) |
Reid Spencer | dee14b5 | 2007-01-31 22:30:26 +0000 | [diff] [blame] | 774 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 775 | Idx))), |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 776 | GetElementPtr, 0, 0, Name, InBe) { |
| 777 | init(Ptr, Idx); |
| 778 | } |
| 779 | |
| 780 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 781 | const std::string &Name, BasicBlock *IAE) |
Reid Spencer | dee14b5 | 2007-01-31 22:30:26 +0000 | [diff] [blame] | 782 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 783 | Idx))), |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 784 | GetElementPtr, 0, 0, Name, IAE) { |
| 785 | init(Ptr, Idx); |
| 786 | } |
| 787 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 788 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 789 | const std::string &Name, Instruction *InBe) |
| 790 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 791 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 792 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 793 | init(Ptr, Idx0, Idx1); |
| 794 | } |
| 795 | |
| 796 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 797 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 798 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 799 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 800 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 801 | init(Ptr, Idx0, Idx1); |
| 802 | } |
| 803 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 804 | GetElementPtrInst::~GetElementPtrInst() { |
| 805 | delete[] OperandList; |
| 806 | } |
| 807 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 808 | // getIndexedType - Returns the type of the element that would be loaded with |
| 809 | // a load instruction with the specified parameters. |
| 810 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 811 | // A null type is returned if the indices are invalid for the specified |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 812 | // pointer type. |
| 813 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 814 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 815 | Value* const *Idxs, |
| 816 | unsigned NumIdx, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 817 | bool AllowCompositeLeaf) { |
| 818 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 819 | |
| 820 | // Handle the special case of the empty set index set... |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 821 | if (NumIdx == 0) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 822 | if (AllowCompositeLeaf || |
| 823 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 824 | return cast<PointerType>(Ptr)->getElementType(); |
| 825 | else |
| 826 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 827 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 828 | unsigned CurIdx = 0; |
| 829 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 830 | if (NumIdx == CurIdx) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 831 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 832 | return 0; // Can't load a whole structure or array!?!? |
| 833 | } |
| 834 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 835 | Value *Index = Idxs[CurIdx++]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 836 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 837 | return 0; // Can only index into pointer types at the first index! |
| 838 | if (!CT->indexValid(Index)) return 0; |
| 839 | Ptr = CT->getTypeAtIndex(Index); |
| 840 | |
| 841 | // If the new type forwards to another type, then it is in the middle |
| 842 | // of being refined to another type (and hence, may have dropped all |
| 843 | // references to what it was using before). So, use the new forwarded |
| 844 | // type. |
| 845 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 846 | Ptr = Ty; |
| 847 | } |
| 848 | } |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 849 | return CurIdx == NumIdx ? Ptr : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 852 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 853 | Value *Idx0, Value *Idx1, |
| 854 | bool AllowCompositeLeaf) { |
| 855 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 856 | if (!PTy) return 0; // Type isn't a pointer type! |
| 857 | |
| 858 | // Check the pointer index. |
| 859 | if (!PTy->indexValid(Idx0)) return 0; |
| 860 | |
| 861 | const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); |
| 862 | if (!CT || !CT->indexValid(Idx1)) return 0; |
| 863 | |
| 864 | const Type *ElTy = CT->getTypeAtIndex(Idx1); |
| 865 | if (AllowCompositeLeaf || ElTy->isFirstClassType()) |
| 866 | return ElTy; |
| 867 | return 0; |
| 868 | } |
| 869 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 870 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 871 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 872 | if (!PTy) return 0; // Type isn't a pointer type! |
| 873 | |
| 874 | // Check the pointer index. |
| 875 | if (!PTy->indexValid(Idx)) return 0; |
| 876 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 877 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 880 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 881 | // ExtractElementInst Implementation |
| 882 | //===----------------------------------------------------------------------===// |
| 883 | |
| 884 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 885 | const std::string &Name, |
| 886 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 887 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 888 | ExtractElement, Ops, 2, Name, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 889 | assert(isValidOperands(Val, Index) && |
| 890 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 891 | Ops[0].init(Val, this); |
| 892 | Ops[1].init(Index, this); |
| 893 | } |
| 894 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 895 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 896 | const std::string &Name, |
| 897 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 898 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 899 | ExtractElement, Ops, 2, Name, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 900 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 901 | assert(isValidOperands(Val, Index) && |
| 902 | "Invalid extractelement instruction operands!"); |
| 903 | Ops[0].init(Val, this); |
| 904 | Ops[1].init(Index, this); |
| 905 | } |
| 906 | |
| 907 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 908 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 909 | const std::string &Name, |
| 910 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 911 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 912 | ExtractElement, Ops, 2, Name, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 913 | assert(isValidOperands(Val, Index) && |
| 914 | "Invalid extractelement instruction operands!"); |
| 915 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 916 | Ops[0].init(Val, this); |
| 917 | Ops[1].init(Index, this); |
| 918 | } |
| 919 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 920 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 921 | const std::string &Name, |
| 922 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 923 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 924 | ExtractElement, Ops, 2, Name, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 925 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 926 | assert(isValidOperands(Val, Index) && |
| 927 | "Invalid extractelement instruction operands!"); |
| 928 | |
| 929 | Ops[0].init(Val, this); |
| 930 | Ops[1].init(Index, this); |
| 931 | } |
| 932 | |
| 933 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 934 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 935 | if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 936 | return false; |
| 937 | return true; |
| 938 | } |
| 939 | |
| 940 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 941 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 942 | // InsertElementInst Implementation |
| 943 | //===----------------------------------------------------------------------===// |
| 944 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 945 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 946 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 947 | Ops[0].init(IE.Ops[0], this); |
| 948 | Ops[1].init(IE.Ops[1], this); |
| 949 | Ops[2].init(IE.Ops[2], this); |
| 950 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 951 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 952 | const std::string &Name, |
| 953 | Instruction *InsertBef) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 954 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
| 955 | assert(isValidOperands(Vec, Elt, Index) && |
| 956 | "Invalid insertelement instruction operands!"); |
| 957 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 958 | Ops[1].init(Elt, this); |
| 959 | Ops[2].init(Index, this); |
| 960 | } |
| 961 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 962 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 963 | const std::string &Name, |
| 964 | Instruction *InsertBef) |
| 965 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 966 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 967 | assert(isValidOperands(Vec, Elt, Index) && |
| 968 | "Invalid insertelement instruction operands!"); |
| 969 | Ops[0].init(Vec, this); |
| 970 | Ops[1].init(Elt, this); |
| 971 | Ops[2].init(Index, this); |
| 972 | } |
| 973 | |
| 974 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 975 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 976 | const std::string &Name, |
| 977 | BasicBlock *InsertAE) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 978 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
| 979 | assert(isValidOperands(Vec, Elt, Index) && |
| 980 | "Invalid insertelement instruction operands!"); |
| 981 | |
| 982 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 983 | Ops[1].init(Elt, this); |
| 984 | Ops[2].init(Index, this); |
| 985 | } |
| 986 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 987 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 988 | const std::string &Name, |
| 989 | BasicBlock *InsertAE) |
| 990 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 991 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 992 | assert(isValidOperands(Vec, Elt, Index) && |
| 993 | "Invalid insertelement instruction operands!"); |
| 994 | |
| 995 | Ops[0].init(Vec, this); |
| 996 | Ops[1].init(Elt, this); |
| 997 | Ops[2].init(Index, this); |
| 998 | } |
| 999 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1000 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1001 | const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1002 | if (!isa<VectorType>(Vec->getType())) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1003 | return false; // First operand of insertelement must be packed type. |
| 1004 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1005 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1006 | return false;// Second operand of insertelement must be packed element type. |
| 1007 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1008 | if (Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1009 | return false; // Third operand of insertelement must be uint. |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
| 1013 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1014 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1015 | // ShuffleVectorInst Implementation |
| 1016 | //===----------------------------------------------------------------------===// |
| 1017 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1018 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 1019 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 1020 | Ops[0].init(SV.Ops[0], this); |
| 1021 | Ops[1].init(SV.Ops[1], this); |
| 1022 | Ops[2].init(SV.Ops[2], this); |
| 1023 | } |
| 1024 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1025 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1026 | const std::string &Name, |
| 1027 | Instruction *InsertBefore) |
| 1028 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) { |
| 1029 | assert(isValidOperands(V1, V2, Mask) && |
| 1030 | "Invalid shuffle vector instruction operands!"); |
| 1031 | Ops[0].init(V1, this); |
| 1032 | Ops[1].init(V2, this); |
| 1033 | Ops[2].init(Mask, this); |
| 1034 | } |
| 1035 | |
| 1036 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1037 | const std::string &Name, |
| 1038 | BasicBlock *InsertAtEnd) |
| 1039 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) { |
| 1040 | assert(isValidOperands(V1, V2, Mask) && |
| 1041 | "Invalid shuffle vector instruction operands!"); |
| 1042 | |
| 1043 | Ops[0].init(V1, this); |
| 1044 | Ops[1].init(V2, this); |
| 1045 | Ops[2].init(Mask, this); |
| 1046 | } |
| 1047 | |
| 1048 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 1049 | const Value *Mask) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1050 | if (!isa<VectorType>(V1->getType())) return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1051 | if (V1->getType() != V2->getType()) return false; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1052 | if (!isa<VectorType>(Mask->getType()) || |
| 1053 | cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty || |
| 1054 | cast<VectorType>(Mask->getType())->getNumElements() != |
| 1055 | cast<VectorType>(V1->getType())->getNumElements()) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1056 | return false; |
| 1057 | return true; |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1062 | // BinaryOperator Class |
| 1063 | //===----------------------------------------------------------------------===// |
| 1064 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1065 | void BinaryOperator::init(BinaryOps iType) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1066 | { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1067 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 1068 | LHS = LHS; RHS = RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1069 | assert(LHS->getType() == RHS->getType() && |
| 1070 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1071 | #ifndef NDEBUG |
| 1072 | switch (iType) { |
| 1073 | case Add: case Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1074 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1075 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1076 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1077 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1078 | isa<VectorType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1079 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1080 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1081 | case UDiv: |
| 1082 | case SDiv: |
| 1083 | assert(getType() == LHS->getType() && |
| 1084 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1085 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1086 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1087 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1088 | break; |
| 1089 | case FDiv: |
| 1090 | assert(getType() == LHS->getType() && |
| 1091 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1092 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1093 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1094 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1095 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1096 | case URem: |
| 1097 | case SRem: |
| 1098 | assert(getType() == LHS->getType() && |
| 1099 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1100 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1101 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1102 | "Incorrect operand type (not integer) for S/UREM"); |
| 1103 | break; |
| 1104 | case FRem: |
| 1105 | assert(getType() == LHS->getType() && |
| 1106 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1107 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1108 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1109 | && "Incorrect operand type (not floating point) for FREM"); |
| 1110 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1111 | case Shl: |
| 1112 | case LShr: |
| 1113 | case AShr: |
| 1114 | assert(getType() == LHS->getType() && |
| 1115 | "Shift operation should return same type as operands!"); |
| 1116 | assert(getType()->isInteger() && |
| 1117 | "Shift operation requires integer operands"); |
| 1118 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1119 | case And: case Or: |
| 1120 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1121 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1122 | "Logical operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1123 | assert((getType()->isInteger() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1124 | (isa<VectorType>(getType()) && |
| 1125 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1126 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1127 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1128 | default: |
| 1129 | break; |
| 1130 | } |
| 1131 | #endif |
| 1132 | } |
| 1133 | |
| 1134 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1135 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1136 | Instruction *InsertBefore) { |
| 1137 | assert(S1->getType() == S2->getType() && |
| 1138 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1139 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1143 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1144 | BasicBlock *InsertAtEnd) { |
| 1145 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1146 | InsertAtEnd->getInstList().push_back(Res); |
| 1147 | return Res; |
| 1148 | } |
| 1149 | |
| 1150 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1151 | Instruction *InsertBefore) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1152 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1153 | return new BinaryOperator(Instruction::Sub, |
| 1154 | zero, Op, |
| 1155 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1159 | BasicBlock *InsertAtEnd) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1160 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1161 | return new BinaryOperator(Instruction::Sub, |
| 1162 | zero, Op, |
| 1163 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1167 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1168 | Constant *C; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1169 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1170 | C = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1171 | C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1172 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1173 | C = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1177 | Op->getType(), Name, InsertBefore); |
| 1178 | } |
| 1179 | |
| 1180 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1181 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1182 | Constant *AllOnes; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1183 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1184 | // Create a vector of all ones values. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1185 | Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1186 | AllOnes = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1187 | ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1188 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1189 | AllOnes = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1193 | Op->getType(), Name, InsertAtEnd); |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | // isConstantAllOnes - Helper function for several functions below |
| 1198 | static inline bool isConstantAllOnes(const Value *V) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1199 | return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | bool BinaryOperator::isNeg(const Value *V) { |
| 1203 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1204 | if (Bop->getOpcode() == Instruction::Sub) |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1205 | return Bop->getOperand(0) == |
| 1206 | ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | bool BinaryOperator::isNot(const Value *V) { |
| 1211 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1212 | return (Bop->getOpcode() == Instruction::Xor && |
| 1213 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1214 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1218 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1219 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1220 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1223 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1224 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1227 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1228 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1229 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1230 | Value *Op0 = BO->getOperand(0); |
| 1231 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1232 | if (isConstantAllOnes(Op0)) return Op1; |
| 1233 | |
| 1234 | assert(isConstantAllOnes(Op1)); |
| 1235 | return Op0; |
| 1236 | } |
| 1237 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1238 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1239 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | // swapOperands - Exchange the two operands to this instruction. This |
| 1244 | // instruction is safe to use on any binary instruction and does not |
| 1245 | // modify the semantics of the instruction. If the instruction is |
| 1246 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1247 | // |
| 1248 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1249 | if (!isCommutative()) |
| 1250 | return true; // Can't commute operands |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1251 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1252 | return false; |
| 1253 | } |
| 1254 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1255 | //===----------------------------------------------------------------------===// |
| 1256 | // CastInst Class |
| 1257 | //===----------------------------------------------------------------------===// |
| 1258 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1259 | // Just determine if this cast only deals with integral->integral conversion. |
| 1260 | bool CastInst::isIntegerCast() const { |
| 1261 | switch (getOpcode()) { |
| 1262 | default: return false; |
| 1263 | case Instruction::ZExt: |
| 1264 | case Instruction::SExt: |
| 1265 | case Instruction::Trunc: |
| 1266 | return true; |
| 1267 | case Instruction::BitCast: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1268 | return getOperand(0)->getType()->isInteger() && getType()->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1269 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1272 | bool CastInst::isLosslessCast() const { |
| 1273 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1274 | if (getOpcode() != Instruction::BitCast) |
| 1275 | return false; |
| 1276 | |
| 1277 | // Identity cast is always lossless |
| 1278 | const Type* SrcTy = getOperand(0)->getType(); |
| 1279 | const Type* DstTy = getType(); |
| 1280 | if (SrcTy == DstTy) |
| 1281 | return true; |
| 1282 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1283 | // Pointer to pointer is always lossless. |
| 1284 | if (isa<PointerType>(SrcTy)) |
| 1285 | return isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1286 | return false; // Other types have no identity values |
| 1287 | } |
| 1288 | |
| 1289 | /// This function determines if the CastInst does not require any bits to be |
| 1290 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1291 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1292 | /// example, the following are all no-op casts: |
| 1293 | /// # bitcast uint %X, int |
| 1294 | /// # bitcast uint* %x, sbyte* |
| 1295 | /// # bitcast packed< 2 x int > %x, packed< 4 x short> |
| 1296 | /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only |
| 1297 | /// @brief Determine if a cast is a no-op. |
| 1298 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1299 | switch (getOpcode()) { |
| 1300 | default: |
| 1301 | assert(!"Invalid CastOp"); |
| 1302 | case Instruction::Trunc: |
| 1303 | case Instruction::ZExt: |
| 1304 | case Instruction::SExt: |
| 1305 | case Instruction::FPTrunc: |
| 1306 | case Instruction::FPExt: |
| 1307 | case Instruction::UIToFP: |
| 1308 | case Instruction::SIToFP: |
| 1309 | case Instruction::FPToUI: |
| 1310 | case Instruction::FPToSI: |
| 1311 | return false; // These always modify bits |
| 1312 | case Instruction::BitCast: |
| 1313 | return true; // BitCast never modifies bits. |
| 1314 | case Instruction::PtrToInt: |
| 1315 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1316 | getType()->getPrimitiveSizeInBits(); |
| 1317 | case Instruction::IntToPtr: |
| 1318 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1319 | getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /// This function determines if a pair of casts can be eliminated and what |
| 1324 | /// opcode should be used in the elimination. This assumes that there are two |
| 1325 | /// instructions like this: |
| 1326 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1327 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1328 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1329 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1330 | /// If no such cast is permited, the function returns 0. |
| 1331 | unsigned CastInst::isEliminableCastPair( |
| 1332 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1333 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1334 | { |
| 1335 | // Define the 144 possibilities for these two cast instructions. The values |
| 1336 | // in this matrix determine what to do in a given situation and select the |
| 1337 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1338 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1339 | // the following cast properties: |
| 1340 | // |
| 1341 | // Size Compare Source Destination |
| 1342 | // Operator Src ? Size Type Sign Type Sign |
| 1343 | // -------- ------------ ------------------- --------------------- |
| 1344 | // TRUNC > Integer Any Integral Any |
| 1345 | // ZEXT < Integral Unsigned Integer Any |
| 1346 | // SEXT < Integral Signed Integer Any |
| 1347 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1348 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1349 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1350 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1351 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1352 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1353 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1354 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1355 | // BITCONVERT = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1356 | // |
| 1357 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
| 1358 | // For example, we could merge "fptoui double to uint" + "zext uint to ulong", |
| 1359 | // into "fptoui double to ulong", but this loses information about the range |
| 1360 | // of the produced value (we no longer know the top-part is all zeros). |
| 1361 | // Further this conversion is often much more expensive for typical hardware, |
| 1362 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1363 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1364 | const unsigned numCastOps = |
| 1365 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1366 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1367 | // T F F U S F F P I B -+ |
| 1368 | // R Z S P P I I T P 2 N T | |
| 1369 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1370 | // N X X U S F F N X N 2 V | |
| 1371 | // C T T I I P P C T T P T -+ |
| 1372 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1373 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1374 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1375 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1376 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1377 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1378 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1379 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1380 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1381 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1382 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1383 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1384 | }; |
| 1385 | |
| 1386 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1387 | [secondOp-Instruction::CastOpsBegin]; |
| 1388 | switch (ElimCase) { |
| 1389 | case 0: |
| 1390 | // categorically disallowed |
| 1391 | return 0; |
| 1392 | case 1: |
| 1393 | // allowed, use first cast's opcode |
| 1394 | return firstOp; |
| 1395 | case 2: |
| 1396 | // allowed, use second cast's opcode |
| 1397 | return secondOp; |
| 1398 | case 3: |
| 1399 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1400 | // is integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1401 | if (DstTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1402 | return firstOp; |
| 1403 | return 0; |
| 1404 | case 4: |
| 1405 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1406 | // is floating point |
| 1407 | if (DstTy->isFloatingPoint()) |
| 1408 | return firstOp; |
| 1409 | return 0; |
| 1410 | case 5: |
| 1411 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1412 | // is an integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1413 | if (SrcTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1414 | return secondOp; |
| 1415 | return 0; |
| 1416 | case 6: |
| 1417 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1418 | // is a floating point |
| 1419 | if (SrcTy->isFloatingPoint()) |
| 1420 | return secondOp; |
| 1421 | return 0; |
| 1422 | case 7: { |
| 1423 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
| 1424 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1425 | unsigned MidSize = MidTy->getPrimitiveSizeInBits(); |
| 1426 | if (MidSize >= PtrSize) |
| 1427 | return Instruction::BitCast; |
| 1428 | return 0; |
| 1429 | } |
| 1430 | case 8: { |
| 1431 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1432 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1433 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
| 1434 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1435 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1436 | if (SrcSize == DstSize) |
| 1437 | return Instruction::BitCast; |
| 1438 | else if (SrcSize < DstSize) |
| 1439 | return firstOp; |
| 1440 | return secondOp; |
| 1441 | } |
| 1442 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1443 | return Instruction::ZExt; |
| 1444 | case 10: |
| 1445 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1446 | // the same as the original, in which case its just a bitcast |
| 1447 | if (SrcTy == DstTy) |
| 1448 | return Instruction::BitCast; |
| 1449 | return 0; // If the types are not the same we can't eliminate it. |
| 1450 | case 11: |
| 1451 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1452 | // is a pointer to pointer cast. |
| 1453 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1454 | return secondOp; |
| 1455 | return 0; |
| 1456 | case 12: |
| 1457 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1458 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1459 | return firstOp; |
| 1460 | return 0; |
| 1461 | case 13: { |
| 1462 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
| 1463 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1464 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1465 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1466 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1467 | return Instruction::BitCast; |
| 1468 | return 0; |
| 1469 | } |
| 1470 | case 99: |
| 1471 | // cast combination can't happen (error in input). This is for all cases |
| 1472 | // where the MidTy is not the same for the two cast instructions. |
| 1473 | assert(!"Invalid Cast Combination"); |
| 1474 | return 0; |
| 1475 | default: |
| 1476 | assert(!"Error in CastResults table!!!"); |
| 1477 | return 0; |
| 1478 | } |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1483 | const std::string &Name, Instruction *InsertBefore) { |
| 1484 | // Construct and return the appropriate CastInst subclass |
| 1485 | switch (op) { |
| 1486 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1487 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1488 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1489 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1490 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1491 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1492 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1493 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1494 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1495 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1496 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1497 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1498 | default: |
| 1499 | assert(!"Invalid opcode provided"); |
| 1500 | } |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
| 1504 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1505 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1506 | // Construct and return the appropriate CastInst subclass |
| 1507 | switch (op) { |
| 1508 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1509 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1510 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1511 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1512 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1513 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1514 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1515 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1516 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1517 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1518 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1519 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1520 | default: |
| 1521 | assert(!"Invalid opcode provided"); |
| 1522 | } |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1526 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1527 | const std::string &Name, |
| 1528 | Instruction *InsertBefore) { |
| 1529 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1530 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1531 | return create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
| 1532 | } |
| 1533 | |
| 1534 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1535 | const std::string &Name, |
| 1536 | BasicBlock *InsertAtEnd) { |
| 1537 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1538 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1539 | return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
| 1540 | } |
| 1541 | |
| 1542 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1543 | const std::string &Name, |
| 1544 | Instruction *InsertBefore) { |
| 1545 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1546 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1547 | return create(Instruction::SExt, S, Ty, Name, InsertBefore); |
| 1548 | } |
| 1549 | |
| 1550 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1551 | const std::string &Name, |
| 1552 | BasicBlock *InsertAtEnd) { |
| 1553 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1554 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1555 | return create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
| 1556 | } |
| 1557 | |
| 1558 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1559 | const std::string &Name, |
| 1560 | Instruction *InsertBefore) { |
| 1561 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1562 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1563 | return create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
| 1564 | } |
| 1565 | |
| 1566 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1567 | const std::string &Name, |
| 1568 | BasicBlock *InsertAtEnd) { |
| 1569 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1570 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1571 | return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
| 1572 | } |
| 1573 | |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1574 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1575 | const std::string &Name, |
| 1576 | BasicBlock *InsertAtEnd) { |
| 1577 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1578 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1579 | "Invalid cast"); |
| 1580 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1581 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1582 | return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 1583 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1584 | } |
| 1585 | |
| 1586 | /// @brief Create a BitCast or a PtrToInt cast instruction |
| 1587 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1588 | const std::string &Name, |
| 1589 | Instruction *InsertBefore) { |
| 1590 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1591 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1592 | "Invalid cast"); |
| 1593 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1594 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1595 | return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 1596 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1597 | } |
| 1598 | |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1599 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1600 | bool isSigned, const std::string &Name, |
| 1601 | Instruction *InsertBefore) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1602 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1603 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1604 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1605 | Instruction::CastOps opcode = |
| 1606 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1607 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1608 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1609 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1610 | } |
| 1611 | |
| 1612 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1613 | bool isSigned, const std::string &Name, |
| 1614 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1615 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1616 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1617 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1618 | Instruction::CastOps opcode = |
| 1619 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1620 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1621 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1622 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1623 | } |
| 1624 | |
| 1625 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1626 | const std::string &Name, |
| 1627 | Instruction *InsertBefore) { |
| 1628 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1629 | "Invalid cast"); |
| 1630 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1631 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1632 | Instruction::CastOps opcode = |
| 1633 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1634 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1635 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1636 | } |
| 1637 | |
| 1638 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1639 | const std::string &Name, |
| 1640 | BasicBlock *InsertAtEnd) { |
| 1641 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1642 | "Invalid cast"); |
| 1643 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1644 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1645 | Instruction::CastOps opcode = |
| 1646 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1647 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1648 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1649 | } |
| 1650 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1651 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 1652 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1653 | // logic in the castIsValid function below. This axiom should hold: |
| 1654 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 1655 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1656 | // casting opcode for the arguments passed to it. |
| 1657 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1658 | CastInst::getCastOpcode( |
| 1659 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1660 | // Get the bit sizes, we'll need these |
| 1661 | const Type *SrcTy = Src->getType(); |
| 1662 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed |
| 1663 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed |
| 1664 | |
| 1665 | // Run through the possibilities ... |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1666 | if (DestTy->isInteger()) { // Casting to integral |
| 1667 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1668 | if (DestBits < SrcBits) |
| 1669 | return Trunc; // int -> smaller int |
| 1670 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1671 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1672 | return SExt; // signed -> SEXT |
| 1673 | else |
| 1674 | return ZExt; // unsigned -> ZEXT |
| 1675 | } else { |
| 1676 | return BitCast; // Same size, No-op cast |
| 1677 | } |
| 1678 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1679 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1680 | return FPToSI; // FP -> sint |
| 1681 | else |
| 1682 | return FPToUI; // FP -> uint |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1683 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1684 | assert(DestBits == PTy->getBitWidth() && |
| 1685 | "Casting packed to integer of different width"); |
| 1686 | return BitCast; // Same size, no-op cast |
| 1687 | } else { |
| 1688 | assert(isa<PointerType>(SrcTy) && |
| 1689 | "Casting from a value that is not first-class type"); |
| 1690 | return PtrToInt; // ptr -> int |
| 1691 | } |
| 1692 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1693 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1694 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1695 | return SIToFP; // sint -> FP |
| 1696 | else |
| 1697 | return UIToFP; // uint -> FP |
| 1698 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 1699 | if (DestBits < SrcBits) { |
| 1700 | return FPTrunc; // FP -> smaller FP |
| 1701 | } else if (DestBits > SrcBits) { |
| 1702 | return FPExt; // FP -> larger FP |
| 1703 | } else { |
| 1704 | return BitCast; // same size, no-op cast |
| 1705 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1706 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1707 | assert(DestBits == PTy->getBitWidth() && |
| 1708 | "Casting packed to floating point of different width"); |
| 1709 | return BitCast; // same size, no-op cast |
| 1710 | } else { |
| 1711 | assert(0 && "Casting pointer or non-first class to float"); |
| 1712 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame^] | 1713 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 1714 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1715 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
| 1716 | "Casting packed to packed of different widths"); |
| 1717 | return BitCast; // packed -> packed |
| 1718 | } else if (DestPTy->getBitWidth() == SrcBits) { |
| 1719 | return BitCast; // float/int -> packed |
| 1720 | } else { |
| 1721 | assert(!"Illegal cast to packed (wrong type or size)"); |
| 1722 | } |
| 1723 | } else if (isa<PointerType>(DestTy)) { |
| 1724 | if (isa<PointerType>(SrcTy)) { |
| 1725 | return BitCast; // ptr -> ptr |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1726 | } else if (SrcTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1727 | return IntToPtr; // int -> ptr |
| 1728 | } else { |
| 1729 | assert(!"Casting pointer to other than pointer or int"); |
| 1730 | } |
| 1731 | } else { |
| 1732 | assert(!"Casting to type that is not first-class"); |
| 1733 | } |
| 1734 | |
| 1735 | // If we fall through to here we probably hit an assertion cast above |
| 1736 | // and assertions are not turned on. Anything we return is an error, so |
| 1737 | // BitCast is as good a choice as any. |
| 1738 | return BitCast; |
| 1739 | } |
| 1740 | |
| 1741 | //===----------------------------------------------------------------------===// |
| 1742 | // CastInst SubClass Constructors |
| 1743 | //===----------------------------------------------------------------------===// |
| 1744 | |
| 1745 | /// Check that the construction parameters for a CastInst are correct. This |
| 1746 | /// could be broken out into the separate constructors but it is useful to have |
| 1747 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 1748 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1749 | bool |
| 1750 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1751 | |
| 1752 | // Check for type sanity on the arguments |
| 1753 | const Type *SrcTy = S->getType(); |
| 1754 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 1755 | return false; |
| 1756 | |
| 1757 | // Get the size of the types in bits, we'll need this later |
| 1758 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1759 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1760 | |
| 1761 | // Switch on the opcode provided |
| 1762 | switch (op) { |
| 1763 | default: return false; // This is an input error |
| 1764 | case Instruction::Trunc: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1765 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1766 | case Instruction::ZExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1767 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1768 | case Instruction::SExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1769 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1770 | case Instruction::FPTrunc: |
| 1771 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1772 | SrcBitSize > DstBitSize; |
| 1773 | case Instruction::FPExt: |
| 1774 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1775 | SrcBitSize < DstBitSize; |
| 1776 | case Instruction::UIToFP: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1777 | return SrcTy->isInteger() && DstTy->isFloatingPoint(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1778 | case Instruction::SIToFP: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1779 | return SrcTy->isInteger() && DstTy->isFloatingPoint(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1780 | case Instruction::FPToUI: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1781 | return SrcTy->isFloatingPoint() && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1782 | case Instruction::FPToSI: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1783 | return SrcTy->isFloatingPoint() && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1784 | case Instruction::PtrToInt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1785 | return isa<PointerType>(SrcTy) && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1786 | case Instruction::IntToPtr: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1787 | return SrcTy->isInteger() && isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1788 | case Instruction::BitCast: |
| 1789 | // BitCast implies a no-op cast of type only. No bits change. |
| 1790 | // However, you can't cast pointers to anything but pointers. |
| 1791 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 1792 | return false; |
| 1793 | |
| 1794 | // Now we know we're not dealing with a pointer/non-poiner mismatch. In all |
| 1795 | // these cases, the cast is okay if the source and destination bit widths |
| 1796 | // are identical. |
| 1797 | return SrcBitSize == DstBitSize; |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | TruncInst::TruncInst( |
| 1802 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1803 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1804 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | TruncInst::TruncInst( |
| 1808 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1809 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1810 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | ZExtInst::ZExtInst( |
| 1814 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1815 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1816 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | ZExtInst::ZExtInst( |
| 1820 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1821 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1822 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1823 | } |
| 1824 | SExtInst::SExtInst( |
| 1825 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1826 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1827 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 1830 | SExtInst::SExtInst( |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1831 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1832 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1833 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | FPTruncInst::FPTruncInst( |
| 1837 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1838 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1839 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | FPTruncInst::FPTruncInst( |
| 1843 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1844 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1845 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | FPExtInst::FPExtInst( |
| 1849 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1850 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1851 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | FPExtInst::FPExtInst( |
| 1855 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1856 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1857 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | UIToFPInst::UIToFPInst( |
| 1861 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1862 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1863 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | UIToFPInst::UIToFPInst( |
| 1867 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1868 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1869 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | SIToFPInst::SIToFPInst( |
| 1873 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1874 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1875 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | SIToFPInst::SIToFPInst( |
| 1879 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1880 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1881 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | FPToUIInst::FPToUIInst( |
| 1885 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1886 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1887 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | FPToUIInst::FPToUIInst( |
| 1891 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1892 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1893 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | FPToSIInst::FPToSIInst( |
| 1897 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1898 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1899 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | FPToSIInst::FPToSIInst( |
| 1903 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1904 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1905 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | PtrToIntInst::PtrToIntInst( |
| 1909 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1910 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1911 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | PtrToIntInst::PtrToIntInst( |
| 1915 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1916 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1917 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | IntToPtrInst::IntToPtrInst( |
| 1921 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1922 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1923 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | IntToPtrInst::IntToPtrInst( |
| 1927 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1928 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1929 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | BitCastInst::BitCastInst( |
| 1933 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1934 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1935 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | BitCastInst::BitCastInst( |
| 1939 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1940 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1941 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1942 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 1943 | |
| 1944 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1945 | // CmpInst Classes |
| 1946 | //===----------------------------------------------------------------------===// |
| 1947 | |
| 1948 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 1949 | const std::string &Name, Instruction *InsertBefore) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1950 | : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1951 | Ops[0].init(LHS, this); |
| 1952 | Ops[1].init(RHS, this); |
| 1953 | SubclassData = predicate; |
| 1954 | if (op == Instruction::ICmp) { |
| 1955 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1956 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 1957 | "Invalid ICmp predicate value"); |
| 1958 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1959 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1960 | assert(Op0Ty == Op1Ty && |
| 1961 | "Both operands to ICmp instruction are not of the same type!"); |
| 1962 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1963 | assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1964 | "Invalid operand types for ICmp instruction"); |
| 1965 | return; |
| 1966 | } |
| 1967 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 1968 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 1969 | "Invalid FCmp predicate value"); |
| 1970 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1971 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1972 | assert(Op0Ty == Op1Ty && |
| 1973 | "Both operands to FCmp instruction are not of the same type!"); |
| 1974 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1975 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1976 | "Invalid operand types for FCmp instruction"); |
| 1977 | } |
| 1978 | |
| 1979 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 1980 | const std::string &Name, BasicBlock *InsertAtEnd) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1981 | : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1982 | Ops[0].init(LHS, this); |
| 1983 | Ops[1].init(RHS, this); |
| 1984 | SubclassData = predicate; |
| 1985 | if (op == Instruction::ICmp) { |
| 1986 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1987 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 1988 | "Invalid ICmp predicate value"); |
| 1989 | |
| 1990 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1991 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1992 | assert(Op0Ty == Op1Ty && |
| 1993 | "Both operands to ICmp instruction are not of the same type!"); |
| 1994 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1995 | assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1996 | "Invalid operand types for ICmp instruction"); |
| 1997 | return; |
| 1998 | } |
| 1999 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 2000 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 2001 | "Invalid FCmp predicate value"); |
| 2002 | const Type* Op0Ty = getOperand(0)->getType(); |
| 2003 | const Type* Op1Ty = getOperand(1)->getType(); |
| 2004 | assert(Op0Ty == Op1Ty && |
| 2005 | "Both operands to FCmp instruction are not of the same type!"); |
| 2006 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2007 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2008 | "Invalid operand types for FCmp instruction"); |
| 2009 | } |
| 2010 | |
| 2011 | CmpInst * |
| 2012 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 2013 | const std::string &Name, Instruction *InsertBefore) { |
| 2014 | if (Op == Instruction::ICmp) { |
| 2015 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 2016 | InsertBefore); |
| 2017 | } |
| 2018 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 2019 | InsertBefore); |
| 2020 | } |
| 2021 | |
| 2022 | CmpInst * |
| 2023 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 2024 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 2025 | if (Op == Instruction::ICmp) { |
| 2026 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 2027 | InsertAtEnd); |
| 2028 | } |
| 2029 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 2030 | InsertAtEnd); |
| 2031 | } |
| 2032 | |
| 2033 | void CmpInst::swapOperands() { |
| 2034 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2035 | IC->swapOperands(); |
| 2036 | else |
| 2037 | cast<FCmpInst>(this)->swapOperands(); |
| 2038 | } |
| 2039 | |
| 2040 | bool CmpInst::isCommutative() { |
| 2041 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2042 | return IC->isCommutative(); |
| 2043 | return cast<FCmpInst>(this)->isCommutative(); |
| 2044 | } |
| 2045 | |
| 2046 | bool CmpInst::isEquality() { |
| 2047 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2048 | return IC->isEquality(); |
| 2049 | return cast<FCmpInst>(this)->isEquality(); |
| 2050 | } |
| 2051 | |
| 2052 | |
| 2053 | ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { |
| 2054 | switch (pred) { |
| 2055 | default: |
| 2056 | assert(!"Unknown icmp predicate!"); |
| 2057 | case ICMP_EQ: return ICMP_NE; |
| 2058 | case ICMP_NE: return ICMP_EQ; |
| 2059 | case ICMP_UGT: return ICMP_ULE; |
| 2060 | case ICMP_ULT: return ICMP_UGE; |
| 2061 | case ICMP_UGE: return ICMP_ULT; |
| 2062 | case ICMP_ULE: return ICMP_UGT; |
| 2063 | case ICMP_SGT: return ICMP_SLE; |
| 2064 | case ICMP_SLT: return ICMP_SGE; |
| 2065 | case ICMP_SGE: return ICMP_SLT; |
| 2066 | case ICMP_SLE: return ICMP_SGT; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) { |
| 2071 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2072 | default: assert(! "Unknown icmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2073 | case ICMP_EQ: case ICMP_NE: |
| 2074 | return pred; |
| 2075 | case ICMP_SGT: return ICMP_SLT; |
| 2076 | case ICMP_SLT: return ICMP_SGT; |
| 2077 | case ICMP_SGE: return ICMP_SLE; |
| 2078 | case ICMP_SLE: return ICMP_SGE; |
| 2079 | case ICMP_UGT: return ICMP_ULT; |
| 2080 | case ICMP_ULT: return ICMP_UGT; |
| 2081 | case ICMP_UGE: return ICMP_ULE; |
| 2082 | case ICMP_ULE: return ICMP_UGE; |
| 2083 | } |
| 2084 | } |
| 2085 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2086 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2087 | switch (pred) { |
| 2088 | default: assert(! "Unknown icmp predicate!"); |
| 2089 | case ICMP_EQ: case ICMP_NE: |
| 2090 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2091 | return pred; |
| 2092 | case ICMP_UGT: return ICMP_SGT; |
| 2093 | case ICMP_ULT: return ICMP_SLT; |
| 2094 | case ICMP_UGE: return ICMP_SGE; |
| 2095 | case ICMP_ULE: return ICMP_SLE; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | bool ICmpInst::isSignedPredicate(Predicate pred) { |
| 2100 | switch (pred) { |
| 2101 | default: assert(! "Unknown icmp predicate!"); |
| 2102 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2103 | return true; |
| 2104 | case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: |
| 2105 | case ICMP_UGE: case ICMP_ULE: |
| 2106 | return false; |
| 2107 | } |
| 2108 | } |
| 2109 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2110 | FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { |
| 2111 | switch (pred) { |
| 2112 | default: |
| 2113 | assert(!"Unknown icmp predicate!"); |
| 2114 | case FCMP_OEQ: return FCMP_UNE; |
| 2115 | case FCMP_ONE: return FCMP_UEQ; |
| 2116 | case FCMP_OGT: return FCMP_ULE; |
| 2117 | case FCMP_OLT: return FCMP_UGE; |
| 2118 | case FCMP_OGE: return FCMP_ULT; |
| 2119 | case FCMP_OLE: return FCMP_UGT; |
| 2120 | case FCMP_UEQ: return FCMP_ONE; |
| 2121 | case FCMP_UNE: return FCMP_OEQ; |
| 2122 | case FCMP_UGT: return FCMP_OLE; |
| 2123 | case FCMP_ULT: return FCMP_OGE; |
| 2124 | case FCMP_UGE: return FCMP_OLT; |
| 2125 | case FCMP_ULE: return FCMP_OGT; |
| 2126 | case FCMP_ORD: return FCMP_UNO; |
| 2127 | case FCMP_UNO: return FCMP_ORD; |
| 2128 | case FCMP_TRUE: return FCMP_FALSE; |
| 2129 | case FCMP_FALSE: return FCMP_TRUE; |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) { |
| 2134 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2135 | default: assert(!"Unknown fcmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2136 | case FCMP_FALSE: case FCMP_TRUE: |
| 2137 | case FCMP_OEQ: case FCMP_ONE: |
| 2138 | case FCMP_UEQ: case FCMP_UNE: |
| 2139 | case FCMP_ORD: case FCMP_UNO: |
| 2140 | return pred; |
| 2141 | case FCMP_OGT: return FCMP_OLT; |
| 2142 | case FCMP_OLT: return FCMP_OGT; |
| 2143 | case FCMP_OGE: return FCMP_OLE; |
| 2144 | case FCMP_OLE: return FCMP_OGE; |
| 2145 | case FCMP_UGT: return FCMP_ULT; |
| 2146 | case FCMP_ULT: return FCMP_UGT; |
| 2147 | case FCMP_UGE: return FCMP_ULE; |
| 2148 | case FCMP_ULE: return FCMP_UGE; |
| 2149 | } |
| 2150 | } |
| 2151 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2152 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2153 | switch (predicate) { |
| 2154 | default: return false; |
| 2155 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2156 | case ICmpInst::ICMP_UGE: return true; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | bool CmpInst::isSigned(unsigned short predicate){ |
| 2161 | switch (predicate) { |
| 2162 | default: return false; |
| 2163 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2164 | case ICmpInst::ICMP_SGE: return true; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2169 | switch (predicate) { |
| 2170 | default: return false; |
| 2171 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2172 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2173 | case FCmpInst::FCMP_ORD: return true; |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2178 | switch (predicate) { |
| 2179 | default: return false; |
| 2180 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2181 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2182 | case FCmpInst::FCMP_UNO: return true; |
| 2183 | } |
| 2184 | } |
| 2185 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2186 | //===----------------------------------------------------------------------===// |
| 2187 | // SwitchInst Implementation |
| 2188 | //===----------------------------------------------------------------------===// |
| 2189 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2190 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2191 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2192 | ReservedSpace = 2+NumCases*2; |
| 2193 | NumOperands = 2; |
| 2194 | OperandList = new Use[ReservedSpace]; |
| 2195 | |
| 2196 | OperandList[0].init(Value, this); |
| 2197 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2200 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2201 | : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()], |
| 2202 | SI.getNumOperands()) { |
| 2203 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2204 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 2205 | OL[i].init(InOL[i], this); |
| 2206 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2210 | SwitchInst::~SwitchInst() { |
| 2211 | delete [] OperandList; |
| 2212 | } |
| 2213 | |
| 2214 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2215 | /// addCase - Add an entry to the switch instruction... |
| 2216 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2217 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2218 | unsigned OpNo = NumOperands; |
| 2219 | if (OpNo+2 > ReservedSpace) |
| 2220 | resizeOperands(0); // Get more space! |
| 2221 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2222 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2223 | NumOperands = OpNo+2; |
| 2224 | OperandList[OpNo].init(OnVal, this); |
| 2225 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | /// removeCase - This method removes the specified successor from the switch |
| 2229 | /// instruction. Note that this cannot be used to remove the default |
| 2230 | /// destination (successor #0). |
| 2231 | /// |
| 2232 | void SwitchInst::removeCase(unsigned idx) { |
| 2233 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2234 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2235 | |
| 2236 | unsigned NumOps = getNumOperands(); |
| 2237 | Use *OL = OperandList; |
| 2238 | |
| 2239 | // Move everything after this operand down. |
| 2240 | // |
| 2241 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2242 | // client might not expect this to happen. The code as it is thrashes the |
| 2243 | // use/def lists, which is kinda lame. |
| 2244 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2245 | OL[i-2] = OL[i]; |
| 2246 | OL[i-2+1] = OL[i+1]; |
| 2247 | } |
| 2248 | |
| 2249 | // Nuke the last value. |
| 2250 | OL[NumOps-2].set(0); |
| 2251 | OL[NumOps-2+1].set(0); |
| 2252 | NumOperands = NumOps-2; |
| 2253 | } |
| 2254 | |
| 2255 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2256 | /// list according to the following behavior: |
| 2257 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 2258 | /// of operation. This grows the number of ops by 1.5 times. |
| 2259 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2260 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2261 | /// |
| 2262 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 2263 | if (NumOps == 0) { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2264 | NumOps = getNumOperands()/2*6; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2265 | } else if (NumOps*2 > NumOperands) { |
| 2266 | // No resize needed. |
| 2267 | if (ReservedSpace >= NumOps) return; |
| 2268 | } else if (NumOps == NumOperands) { |
| 2269 | if (ReservedSpace == NumOps) return; |
| 2270 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2271 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | ReservedSpace = NumOps; |
| 2275 | Use *NewOps = new Use[NumOps]; |
| 2276 | Use *OldOps = OperandList; |
| 2277 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2278 | NewOps[i].init(OldOps[i], this); |
| 2279 | OldOps[i].set(0); |
| 2280 | } |
| 2281 | delete [] OldOps; |
| 2282 | OperandList = NewOps; |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2287 | return getSuccessor(idx); |
| 2288 | } |
| 2289 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2290 | return getNumSuccessors(); |
| 2291 | } |
| 2292 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2293 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2294 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2295 | |
| 2296 | |
| 2297 | // Define these methods here so vtables don't get emitted into every translation |
| 2298 | // unit that uses these classes. |
| 2299 | |
| 2300 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 2301 | return new GetElementPtrInst(*this); |
| 2302 | } |
| 2303 | |
| 2304 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2305 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2308 | CmpInst* CmpInst::clone() const { |
Reid Spencer | fcb0dd3 | 2006-12-07 04:18:31 +0000 | [diff] [blame] | 2309 | return create(getOpcode(), getPredicate(), Ops[0], Ops[1]); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2312 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 2313 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
| 2314 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
| 2315 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 2316 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 2317 | CastInst *TruncInst::clone() const { return new TruncInst(*this); } |
| 2318 | CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } |
| 2319 | CastInst *SExtInst::clone() const { return new SExtInst(*this); } |
| 2320 | CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } |
| 2321 | CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } |
| 2322 | CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } |
| 2323 | CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } |
| 2324 | CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } |
| 2325 | CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } |
| 2326 | CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } |
| 2327 | CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } |
| 2328 | CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } |
| 2329 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2330 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
| 2331 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
| 2332 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2333 | ExtractElementInst *ExtractElementInst::clone() const { |
| 2334 | return new ExtractElementInst(*this); |
| 2335 | } |
| 2336 | InsertElementInst *InsertElementInst::clone() const { |
| 2337 | return new InsertElementInst(*this); |
| 2338 | } |
| 2339 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 2340 | return new ShuffleVectorInst(*this); |
| 2341 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2342 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 2343 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 2344 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 2345 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 2346 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 2347 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 2348 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |