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