16
16
#include " llvm/ADT/SmallVector.h"
17
17
#include " llvm/CodeGen/Passes.h"
18
18
#include " llvm/IR/IRBuilder.h"
19
+ #include " llvm/IR/InstrTypes.h"
19
20
#include " llvm/IR/Instruction.h"
20
21
#include " llvm/IR/Instructions.h"
21
22
#include " llvm/IR/Intrinsics.h"
@@ -45,6 +46,7 @@ static bool isIntrinsicExpansion(Function &F) {
45
46
case Intrinsic::dx_length:
46
47
case Intrinsic::dx_sdot:
47
48
case Intrinsic::dx_udot:
49
+ case Intrinsic::dx_sign:
48
50
return true ;
49
51
}
50
52
return false ;
@@ -293,6 +295,36 @@ static bool expandClampIntrinsic(CallInst *Orig, Intrinsic::ID ClampIntrinsic) {
293
295
return true ;
294
296
}
295
297
298
+ static bool expandSignIntrinsic (CallInst *Orig) {
299
+ IRBuilder<> Builder (Orig->getParent ());
300
+ Value *X = Orig->getOperand (0 );
301
+ Type *Ty = X->getType ();
302
+ Type *ScalarTy = Ty->getScalarType ();
303
+ Type *RetTy = Orig->getType ();
304
+ Constant *Zero = Constant::getNullValue (Ty);
305
+ Builder.SetInsertPoint (Orig);
306
+
307
+ Value *GT;
308
+ Value *LT;
309
+ if (ScalarTy->isFloatingPointTy ()) {
310
+ GT = Builder.CreateFCmpOLT (Zero, X);
311
+ LT = Builder.CreateFCmpOLT (X, Zero);
312
+ } else {
313
+ assert (ScalarTy->isIntegerTy ());
314
+ GT = Builder.CreateICmpSLT (Zero, X);
315
+ LT = Builder.CreateICmpSLT (X, Zero);
316
+ }
317
+
318
+ Value *ZextGT = Builder.CreateZExt (GT, RetTy);
319
+ Value *ZextLT = Builder.CreateZExt (LT, RetTy);
320
+
321
+ Value *Ret = Builder.CreateSub (ZextGT, ZextLT);
322
+
323
+ Orig->replaceAllUsesWith (Ret);
324
+ Orig->eraseFromParent ();
325
+ return true ;
326
+ }
327
+
296
328
static bool expandIntrinsic (Function &F, CallInst *Orig) {
297
329
switch (F.getIntrinsicID ()) {
298
330
case Intrinsic::abs:
@@ -317,6 +349,8 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
317
349
case Intrinsic::dx_sdot:
318
350
case Intrinsic::dx_udot:
319
351
return expandIntegerDot (Orig, F.getIntrinsicID ());
352
+ case Intrinsic::dx_sign:
353
+ return expandSignIntrinsic (Orig);
320
354
}
321
355
return false ;
322
356
}
0 commit comments