1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef rr_Nucleus_hpp 16 #define rr_Nucleus_hpp 17 18 #include <cassert> 19 #include <cstdarg> 20 #include <cstdint> 21 #include <vector> 22 23 namespace rr 24 { 25 class Type; 26 class Value; 27 class SwitchCases; 28 class BasicBlock; 29 class Routine; 30 31 enum Optimization 32 { 33 Disabled = 0, 34 InstructionCombining = 1, 35 CFGSimplification = 2, 36 LICM = 3, 37 AggressiveDCE = 4, 38 GVN = 5, 39 Reassociate = 6, 40 DeadStoreElimination = 7, 41 SCCP = 8, 42 ScalarReplAggregates = 9, 43 44 OptimizationCount 45 }; 46 47 extern Optimization optimization[10]; 48 49 class Nucleus 50 { 51 public: 52 Nucleus(); 53 54 virtual ~Nucleus(); 55 56 Routine *acquireRoutine(const char *name, bool runOptimizations = true); 57 58 static Value *allocateStackVariable(Type *type, int arraySize = 0); 59 static BasicBlock *createBasicBlock(); 60 static BasicBlock *getInsertBlock(); 61 static void setInsertBlock(BasicBlock *basicBlock); 62 63 static void createFunction(Type *ReturnType, std::vector<Type*> &Params); 64 static Value *getArgument(unsigned int index); 65 66 // Terminators 67 static void createRetVoid(); 68 static void createRet(Value *V); 69 static void createBr(BasicBlock *dest); 70 static void createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse); 71 72 // Binary operators 73 static Value *createAdd(Value *lhs, Value *rhs); 74 static Value *createSub(Value *lhs, Value *rhs); 75 static Value *createMul(Value *lhs, Value *rhs); 76 static Value *createUDiv(Value *lhs, Value *rhs); 77 static Value *createSDiv(Value *lhs, Value *rhs); 78 static Value *createFAdd(Value *lhs, Value *rhs); 79 static Value *createFSub(Value *lhs, Value *rhs); 80 static Value *createFMul(Value *lhs, Value *rhs); 81 static Value *createFDiv(Value *lhs, Value *rhs); 82 static Value *createURem(Value *lhs, Value *rhs); 83 static Value *createSRem(Value *lhs, Value *rhs); 84 static Value *createFRem(Value *lhs, Value *rhs); 85 static Value *createShl(Value *lhs, Value *rhs); 86 static Value *createLShr(Value *lhs, Value *rhs); 87 static Value *createAShr(Value *lhs, Value *rhs); 88 static Value *createAnd(Value *lhs, Value *rhs); 89 static Value *createOr(Value *lhs, Value *rhs); 90 static Value *createXor(Value *lhs, Value *rhs); 91 92 // Unary operators 93 static Value *createNeg(Value *V); 94 static Value *createFNeg(Value *V); 95 static Value *createNot(Value *V); 96 97 // Memory instructions 98 static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); 99 static Value *createStore(Value *value, Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); 100 static Value *createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex); 101 102 // Atomic instructions 103 static Value *createAtomicAdd(Value *ptr, Value *value); 104 105 // Cast/Conversion Operators 106 static Value *createTrunc(Value *V, Type *destType); 107 static Value *createZExt(Value *V, Type *destType); 108 static Value *createSExt(Value *V, Type *destType); 109 static Value *createFPToSI(Value *V, Type *destType); 110 static Value *createSIToFP(Value *V, Type *destType); 111 static Value *createFPTrunc(Value *V, Type *destType); 112 static Value *createFPExt(Value *V, Type *destType); 113 static Value *createBitCast(Value *V, Type *destType); 114 115 // Compare instructions 116 static Value *createICmpEQ(Value *lhs, Value *rhs); 117 static Value *createICmpNE(Value *lhs, Value *rhs); 118 static Value *createICmpUGT(Value *lhs, Value *rhs); 119 static Value *createICmpUGE(Value *lhs, Value *rhs); 120 static Value *createICmpULT(Value *lhs, Value *rhs); 121 static Value *createICmpULE(Value *lhs, Value *rhs); 122 static Value *createICmpSGT(Value *lhs, Value *rhs); 123 static Value *createICmpSGE(Value *lhs, Value *rhs); 124 static Value *createICmpSLT(Value *lhs, Value *rhs); 125 static Value *createICmpSLE(Value *lhs, Value *rhs); 126 static Value *createFCmpOEQ(Value *lhs, Value *rhs); 127 static Value *createFCmpOGT(Value *lhs, Value *rhs); 128 static Value *createFCmpOGE(Value *lhs, Value *rhs); 129 static Value *createFCmpOLT(Value *lhs, Value *rhs); 130 static Value *createFCmpOLE(Value *lhs, Value *rhs); 131 static Value *createFCmpONE(Value *lhs, Value *rhs); 132 static Value *createFCmpORD(Value *lhs, Value *rhs); 133 static Value *createFCmpUNO(Value *lhs, Value *rhs); 134 static Value *createFCmpUEQ(Value *lhs, Value *rhs); 135 static Value *createFCmpUGT(Value *lhs, Value *rhs); 136 static Value *createFCmpUGE(Value *lhs, Value *rhs); 137 static Value *createFCmpULT(Value *lhs, Value *rhs); 138 static Value *createFCmpULE(Value *lhs, Value *rhs); 139 static Value *createFCmpUNE(Value *lhs, Value *rhs); 140 141 // Vector instructions 142 static Value *createExtractElement(Value *vector, Type *type, int index); 143 static Value *createInsertElement(Value *vector, Value *element, int index); 144 static Value *createShuffleVector(Value *V1, Value *V2, const int *select); 145 146 // Other instructions 147 static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse); 148 static SwitchCases *createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases); 149 static void addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch); 150 static void createUnreachable(); 151 152 // Constant values 153 static Value *createNullValue(Type *type); 154 static Value *createConstantLong(int64_t i); 155 static Value *createConstantInt(int i); 156 static Value *createConstantInt(unsigned int i); 157 static Value *createConstantBool(bool b); 158 static Value *createConstantByte(signed char i); 159 static Value *createConstantByte(unsigned char i); 160 static Value *createConstantShort(short i); 161 static Value *createConstantShort(unsigned short i); 162 static Value *createConstantFloat(float x); 163 static Value *createNullPointer(Type *type); 164 static Value *createConstantVector(const int64_t *constants, Type *type); 165 static Value *createConstantVector(const double *constants, Type *type); 166 167 static Type *getPointerType(Type *elementType); 168 169 private: 170 void optimize(); 171 }; 172 } 173 174 #endif // rr_Nucleus_hpp 175